0

Is it possible to fire option click programmatically with pure Javascript?

HTML:

<select id="racetype" class="select-menu" name="race-type">
            <option value="value" class="select-option js-select-option racetype-all" data-filter-value=".js-find-race-item">Race Types</option>
            <option value="value" class="select-option js-select-option racetype-sprint" data-filter-value=".js-type-sprint">Sprint</option>
            <option value="value" class="select-option js-select-option racetype-super" data-filter-value=".js-type-super">Super</option>
            <option value="value" class="select-option js-select-option racetype-beast" data-filter-value=".js-type-beast">Beast</option>
</select>

<a href="javascript:void(0)" onclick="SOsprint();">Click to select the second option (Sprint)</a>

JAVASCRIPT:

function SOsprint() {
    var select = document.getElementById("racetype");
    select.selectedIndex = 1;
    return false;
}
Naourass Derouichi
  • 773
  • 3
  • 12
  • 38
  • possible duplicate of : http://stackoverflow.com/questions/8140862/how-to-select-a-value-in-dropdown-javascript – Janak May 01 '14 at 05:18
  • hi dear check this url http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_option_index – Prashant May 01 '14 at 06:33

2 Answers2

1

You don't need [0]. getElementById() returns just a single element and not a NodeList

Do this:

var select = document.getElementById("racetype");
Amit Joki
  • 58,320
  • 7
  • 77
  • 95
0

There is no need to use [0] as document.getElementById returns a reference to the element by its ID not an array.

Use

 var select = document.getElementById("racetype");

instead of

var select = document.getElementById("racetype")[0];
Satpal
  • 132,252
  • 13
  • 159
  • 168