0

I'm using Office UI Fabric and I'm using a ms-Dropdown control. From javascript, I'm trying to select and item

<div class="ms-Dropdown" tabindex="0">
  <label class="ms-Label" style="font-size: 14px; font-weight: 600;">My Options</label>
  <i class="ms-Dropdown-caretDown ms-Icon ms-Icon--ChevronDown"></i>
  <select id="myoptions" class="ms-Dropdown-select">
    <option>Choose an option&amp;hellip;</option>
    <option>One</option>
    <option>Two</option>
  </select>
</div>

From Javascript, I run

document.getElementById("myoptions").value = "Two";

or

document.getElementById("myoptions").selectedIndex = 2;

Both don't work properly. The UI don't refresh with the new selected option.

Any idea how can I select an item from Javascript (no jQuery)?

1
  • document.getElementById("myoptions").selectedIndex = 2; should work!!! Commented Jul 25, 2019 at 8:34

3 Answers 3

1

Your options are missing values:

 <select id="myoptions" class="ms-Dropdown-select">
    <option value=''>Choose an option&amp;hellip;</option>
    <option value='One'>One</option>
    <option value='Two'>Two</option>
  </select>
Sign up to request clarification or add additional context in comments.

2 Comments

document.getElementById("myoptions").selectedIndex = 2; should work!!!
But UI is not updated with the selected value. The first option is showed, not updated with new selected option.
1

Use this solution :

 //set item index
 var option = document.getElementById("myoptions").item(2)
 option.setAttribute("selected",true)
 
 
 
 
 
   <div class="ms-Dropdown" tabindex="0">
  <label class="ms-Label" style="font-size: 14px; font-weight: 600;">My Options</label>
  <i class="ms-Dropdown-caretDown ms-Icon ms-Icon--ChevronDown"></i>
  <select id="myoptions" class="ms-Dropdown-select">
    <option>Choose an option&amp;hellip;</option>
    <option >One</option>
    <option >Two</option>
  </select>
</div>

Comments

0

try this:

document.getElementById("myoptions").options.selectedIndex = 2;

Comments

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.