Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
60
rated 0 times [  63] [ 3]  / answers: 1 / hits: 45851  / 10 Years ago, tue, april 22, 2014, 12:00:00
<select id=comboBox>
<option id=1>One</option>
<option id=2>Two</option>
<option id=3>Three</option>
<option id=4>Four</option>
</select>


I want to select option with ID 3. Is there any way how to do it without loop? I expect something like this



$(#comboBox).val(3);


but with ID instead of value. (So I mean to access that option as member of comboBox, not through selector like document.getElementById('3');)


More From » html

 Answers
3
<script type=text/javascript>
document.getElementById(3).selected=true;
</script>


or



<script type=text/javascript>
document.getElementById(comboBox).options[2].selected=true;
</script>


or



<script type=text/javascript>
document.getElementById(comboBox).selectedIndex=2;
</script>


or



<script type=text/javascript>
document.getElementById(comboBox).options.namedItem(3).selected=true;
</script>


For the last one see https://developer.mozilla.org/en/docs/Web/API/HTMLSelectElement#namedItem() and
http://www.w3schools.com/jsref/coll_select_options.asp



You could skip the reference to options for the shorter document.getElementById(comboBox).namedItem(3).selected=true;


[#71352] Sunday, April 20, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
grayson

Total Points: 36
Total Questions: 113
Total Answers: 95

Location: Tonga
Member since Fri, Aug 21, 2020
4 Years ago
grayson questions
;