0

I am developing a Web application. In my application, I am trying to select multiple options for the select dropdown using JavaScript.

This is my HTML

<select id="my-dropdown"
        name="select_locations"
        multiple="multiple"
        class="my-select-control">
        //options are here
</select>

This is my JavaScript

let dropDown = document.getElementById('my-dropdown');
dropDown.options[0].selected = true
dropDown.options[1].selected = true

But it is not selecting the options. How can I select multiple options?

4
  • 2
    Possible duplicate of Javascript to Select Multiple options Commented Sep 25, 2019 at 19:10
  • 1
    also, your code works fine. jsfiddle.net/zmjfr5ek Commented Sep 25, 2019 at 19:11
  • 1
    possibly your js executed before dom rendered Commented Sep 25, 2019 at 19:18
  • It works as expected. Must be something to do with the VueJs life cycle. Commented Sep 25, 2019 at 19:25

0

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.