1

I have a form with select options :

        <div>
            <select>
                <option v-model="department" :value="n" v-for="n in ['Please select', 'Medicine', 'Dental']">{{n}}</option>
            </select>
        </div>
        <div class="alignBtn">
            <label><span>&nbsp;</span><input type="submit" v-on:click.prevent="generateSlip()" value="Submit" />
            </label>
        </div>

and based on the selection in the above I want to display header content:

               <div v-if="{department} === 'Medicine'">
                    <h1>Option A</h1>
                </div>
                <div v-else>
                    <h1>Option B</h1>
                </div>

but every time Option B is getting outputted .

2
  • Is there any special reason for using '{}' around 'department' in v-if Commented Dec 23, 2018 at 7:53
  • you mean just department should render the value of department ? Commented Dec 23, 2018 at 8:03

1 Answer 1

0

I think that the v-model directive should be in the select element. You probably meant to do this ..

<div>
    <select v-model="department">
        <option :value="n" v-for="n in ['Please select', 'Medicine', 'Dental']">{{n}}</option>
    </select>
</div>
<div class="alignBtn">
    <label><span>&nbsp;</span><input type="submit" v-on:click.prevent="generateSlip()" value="Submit" />
    </label>
</div>

You also don't need destructuring in this case. So you can use department in your equality comparison directly ..

<div v-if="department === 'Medicine'">
    <h1>Option A</h1>
</div>
<div v-else>
    <h1>Option B</h1>
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

I see, make sense thanks, can you also help me with this: stackoverflow.com/q/53902123/3311276 please
Also you don't need strict comparison here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.