Archive for May 13th, 2008

How to select the first option in a combo box?

Tuesday, May 13th, 2008

Simply in jQuery (a Javascript framework – Write less, do more):

?View Code JAVASCRIPT
1
$("#div_id option:first).attr("selected","selected"); 

Example: 

?View Code JAVASCRIPT
1
2
3
4
5
6
7
8
$('#myCombo').load(
                              'my_combo.php?foo=bar',
                               function() {
                                     $('#myCombo option:first).attr("selected","selected");
                               }
                      );
html:
<select id="myCombo"></select>