Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript - set select option value to null

Tags:

javascript

I have to disable all the select menus in a div and also set their value to null.

I am using the following code to disable the select menus:

  var div_contents =document.getElementById("div1");
  var elements = div_contents.getElementsByTagName("select");
  for (i=0; i<elements.length; i++) {
      elements[i].disabled = true;
  }

However, I am not able to set the value of the select tag to null.

Also I don't have the null option inside the select tag:

<select id="test1" name="test1">
    <option value="a">aa</option>
    <option value="b">bb</option>
    <option value="c">cc</option>
</select>

How can I set the value of a select list to null, given that I don't have the null option?

like image 238
tanya Avatar asked Oct 19 '25 07:10

tanya


2 Answers

If you want to show no selection, you should set its selectedIndex to -1.

elements[i].selectedIndex = -1;
like image 156
RightSaidFred Avatar answered Oct 21 '25 21:10

RightSaidFred


You could try setting the element's value property:

elements[i].value = '';
like image 26
Blender Avatar answered Oct 21 '25 20:10

Blender