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?
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?
- It's not clear to me what you mean. Do you want to remove all of the options completely? – Wayne Commented Dec 12, 2011 at 17:05
- your code is right, you are just disabling the boxed to set the value null use elements[i].value = '' – Punit Commented Dec 12, 2011 at 17:08
- Using 'elements[i].selectedIndex = -1;' seems to have done the trick – tanya Commented Dec 13, 2011 at 10:47
3 Answers
Reset to default 14If you want to show no selection, you should set its selectedIndex
to -1
.
elements[i].selectedIndex = -1;
You could try setting the element's value
property:
elements[i].value = '';
This one is working. I have tested it.
elements[i].value = ' ';
elements[i].trigger('change');