Skip to content
Advertisement

How to use placeholder in select box if it is created with v-for?

Update:

Due to my own fault of not mentioning, that tool is created in a v-for loop during render, I couldn’t apply the proposed answers although they might have been correct.

In the end I did a workaround by adding following css sudo element to the parent of the select element:

JavaScript

and setting it to display: none, when an item is selected.

Original Question:

I’m creating a select element in vue 3 with help of v-for:

JavaScript

Now I want to set a placeholder text in the select box to Paragraph. Usually it would work like this:

JavaScript

but v-for removes the option of setting selected manually so how do I do it?

Advertisement

Answer

A <select>‘s placeholder is usually disabled and not one of the available options. You can do that with an <option> before your v-for of selectable <option>s:

JavaScript

demo 1

If you actually want to set the <select>‘s initial value, you could set the <select>‘s model (i.e., tool.action) to the desired value:

JavaScript

demo 2

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement