Entry
How can I disable a listitem of a dropdownlistbox in javascript.
Apr 8th, 2008 18:56
ha mo, Dave Clark, dileep desamsetty,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dileep,
As of IE6, individual OPTIONs of an HTML SELECT element can be
disabled but this doesn't prevent their selection. Thus, it must be
handled programmatically -- as the following code demonstrates.
<form action="">
<fieldset>
<select size="1" onchange="
if (this.options[this.selectedIndex].disabled) {
this.selectedIndex = 0;
return false;
}
alert(this.options[this.selectedIndex].text)
return true;">
<option>One</option>
<option disabled="disabled">Two</option>
<option>Three</option>
<option disabled="disabled">Four</option>
<option>Five</option>
<option disabled="disabled">Six</option>
</select>
</fieldset>
</form>
Beyond that, yes, you can programmtically disable and enable
individual options, too. For example:
var opt = document.forms['formName'].elements['selectName'].options;
opt[x].disabled = true; // option is disabled
opt[x].disabled = false; // option is enabled
Take care,
Dave Clark
www.DaveClarkConsulting.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com