Javascript to select first option of select list

Amna Ahmed picture Amna Ahmed · Mar 2, 2012 · Viewed 55.7k times · Source

I need to create a reset button which on click will set all select lists in a form to index 0. I have tried this code for function, but it gives a syntax error on myForm.length;

<script type="text/javascript">function ResetForm(form) {     
var myForm = document.forms[form];   
  for( var i=0; i < myForm.length; i++ ){                                   
myForm.select[i].selectedIndex =0;     }  }

</script>

Answer

Another Code picture Another Code · Mar 2, 2012

There is no such property as someForm.select, try this instead:

selectTags = myForm.getElementsByTagName("select");

for(var i = 0; i < selectTags.length; i++) {
  selectTags[i].selectedIndex =0;
}