|
// Get all options within <select id='foo'>...</select> var op = document.getElementById("foo").getElementsByTagName("option"); for (var i = 0; i < op.length; i++) { // lowercase comparison for case-insensitivity (op[i].value.toLowerCase() == "stackoverflow") ? op[i].disabled = true : op[i].disabled = false ; } |
Without enabling non-targeted elements:
|
// Get all options within <select id='foo'>...</select> var op = document.getElementById("foo").getElementsByTagName("option"); for (var i = 0; i < op.length; i++) { // lowercase comparison for case-insensitivity if (op[i].value.toLowerCase() == "stackoverflow") { op[i].disabled = true; } } |
jQuery
With jQuery you can do this with a single line:
|
$("option[value='optionvalue']") .attr("disabled", "disabled") .siblings().removeAttr("disabled"); |
Without enabling non-targeted elements:
|
$("option[value='optionvalue']").attr("disabled", "disabled"); |
Note that this
is not case insensitive. “optionvalue” will not equal “optionvalue”. To get a case-insensitive match, you’d have to cycle through each, converting the value to a lower case, and
Read More