最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to add "selected" to select option using javascript - Stack Overflow

programmeradmin6浏览0评论

I have the following code and I am trying to add "selected" to the option dynamically, when the user select an option. How can i do it using Javascript ? Example when the user select "Candy" I want to add <option value="candy" selected>Candy</option>

function urlDirect() {
  var businessTypeSelected = document.getElementById("BusinessType").value;
  //alert("x " +x);
  if (businessTypeSelected != "") {
    window.location.href = location.host + businessTypeSelected;
    document.getElementById("BusinessType").selectedIndex = document.getElementById("BusinessType").selectedIndex;
  } else {

  }
}
<span class="custom-dropdown custom-dropdown--blue custom-dropdown--large">
  <select id="BusinessType" class="custom-dropdown__select custom-dropdown__select--blue" onChange="urlDirect()">
    <option value="default">Select your business type</option>
    <option value="auto">Auto </option>
    <option value="aero">Aeroplane</option>
    <option value="film">Film</option>
    <option value="candy">Candy</option>
  </select>
</span>

I have the following code and I am trying to add "selected" to the option dynamically, when the user select an option. How can i do it using Javascript ? Example when the user select "Candy" I want to add <option value="candy" selected>Candy</option>

function urlDirect() {
  var businessTypeSelected = document.getElementById("BusinessType").value;
  //alert("x " +x);
  if (businessTypeSelected != "") {
    window.location.href = location.host + businessTypeSelected;
    document.getElementById("BusinessType").selectedIndex = document.getElementById("BusinessType").selectedIndex;
  } else {

  }
}
<span class="custom-dropdown custom-dropdown--blue custom-dropdown--large">
  <select id="BusinessType" class="custom-dropdown__select custom-dropdown__select--blue" onChange="urlDirect()">
    <option value="default">Select your business type</option>
    <option value="auto">Auto </option>
    <option value="aero">Aeroplane</option>
    <option value="film">Film</option>
    <option value="candy">Candy</option>
  </select>
</span>

Share Improve this question edited Jul 10, 2015 at 15:37 Jason Cust 10.9k2 gold badges36 silver badges45 bronze badges asked Jul 10, 2015 at 15:32 user244394user244394 13.5k25 gold badges84 silver badges142 bronze badges 1
  • Possible duplicate – ODelibalta Commented Jul 10, 2015 at 15:39
Add a ment  | 

2 Answers 2

Reset to default 5

This should do:

var select = document.getElementById('BusinessType');
select.addEventListener('change', function() {

  select.options[select.selectedIndex].setAttribute('selected');
});

Also I'd suggest you change the name of the id to business-type since CSS isn't written in camelCase.

var select = document.getElementById('BusinessType');
select.options[indexOfoption].selected = true;

You can do it by this method too. it's easy to understand

发布评论

评论列表(0)

  1. 暂无评论