tanya
tanya

Reputation: 2985

javascript - set select option value to null

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?

Upvotes: 3

Views: 40349

Answers (3)

Suneel Kumar Verma
Suneel Kumar Verma

Reputation: 1

This one is working. I have tested it.

elements[i].value = ' ';

elements[i].trigger('change'); 

Upvotes: 0

RightSaidFred
RightSaidFred

Reputation: 11327

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

elements[i].selectedIndex = -1;

Upvotes: 14

Blender
Blender

Reputation: 298512

You could try setting the element's value property:

elements[i].value = '';

Upvotes: 3

Related Questions