question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Vue Selected Bind Does Not Work on Option Tags

See original GitHub issue

Version

2.5.16

Reproduction link

https://codepen.io/jbenner/pen/geJqex

Steps to reproduce

  1. Instantiate a Vue instance with a data property named locations which is an array consisting of ['all', 'north', 'south', 'east', 'west'].
  2. Iterate over the locations in an option tag like so <option v-for="location in locations" :selected="location === 'all'">{{ location }}</option>.

What is expected?

The selected attribute to be set on the “all” option element.

What is actually happening?

The selected attribute is not being set.


I’ve also tried the following while attempting to debug but to no avail:

<option v-for="(location, index) in locations" :selected="index === 0">{{ location }}</option>

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:3
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
tony19commented, May 17, 2021

One workaround is to create a directive (named "attr") that sets the attribute:

Vue.directive('attr', (el, binding) => {
  // Boolean attributes take the empty string as a `true` value
  if (binding.value === true) binding.value = ''

  if (binding.value === '' || binding.value) {
    el.setAttribute(binding.arg, binding.value)
  }
})

Then use it in your template like v-bind but with v-attr:

<option v-attr:selected="location === 'all'">

updated codepen

https://stackoverflow.com/a/67577063/6277151

0reactions
daverobertscommented, Feb 20, 2019

I’m running into a similar issue on a select box where I can’t use v-model. Is there a workaround or fix for this issue?

Read more comments on GitHub >

github_iconTop Results From Across the Web

vue.js - Vuejs v-on:click not working on option tag
Instead of binding your selected option to a property of the display object, just bind it to the display object itself.
Read more >
Form Input Bindings - Vue.js
You can use the v-model directive to create two-way data bindings on form input, textarea, and select elements. It automatically picks the correct...
Read more >
How to Use the Select Tag with Vue - Mastering JS
Here's how you can use Vue with the `select` tag, including how to use two way data binding with `v-model`.
Read more >
Vue.js Form Input Binding with Select option - GeeksforGeeks
Form Input Binding with Select option is used to handle the value binding for the select element. The select element uses the value...
Read more >
Bind select to vue - Material Design for Bootstrap
selected ); } } }); If i do not use mdb-select this code works, how can i get it to work with your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found