Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

67
Vistas
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:

&::after {
  position: absolute;
  content: 'Paragraph';
  margin-right: .83rem;
  font-size: .83rem;
  place-self: center;
}

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:

<template>
    <select v-model="tool.action" @change="selectType(tool.action)">
        <option v-for="option in options" :key="option" :value="option.value">{{ option.text }}</option>
    </select>
</template>

<script setup>
const options: [
    { text: 'Heading 1', value: 'h1' },
    { text: 'Heading 2', value: 'h2' },
    { text: 'Heading 3', value: 'h3' },
    { text: 'Paragraph', value: 'p' },
]
</script>

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

<select>
    <option value="p" selected>Paragraph</option>
</select>

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

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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:

<template>
    <select v-model="tool.action" @change="selectType(tool.action)">
        <option disabled value="">Placeholder Option</option> 👈
        <option v-for="option in options" :key="option" :value="option.value">{{ option.text }}</option>
    </select>
</template>

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:

<script setup>
⋮
tool.action = 'p' // initialize to 'Paragraph' option's value
</script>

demo 2

about 4 years ago · Juan Pablo Isaza Denunciar

0

<template>
    <select v-model="tool.action" @change="selectType(tool.action)">
        <option value="p">Paragraph</option>
        <option v-for="option in options" :key="option" :value="option.value">{{ option.text }}</option>
    </select>
</template>

and then, set the default value of tool.action to "p".

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda