Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript to select first option of select list

Tags:

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>
like image 527
Amna Ahmed Avatar asked Mar 02 '12 20:03

Amna Ahmed


People also ask

How do you select the first selection option in HTML?

The option tag contains the value that would be used when selected. The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute. The option that is having the 'selected' attribute will be displayed by default on the dropdown list.

How do I select the first select option in jQuery?

<select> <option value="">Please select an option below</option> ... then you can just use: $('select'). val('');


1 Answers

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;
}  
like image 153
Another Code Avatar answered Oct 10 '22 03:10

Another Code