// 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; } }
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 then check against that:
$("option").each(function(){ if ($(this).val().toLowerCase() == "stackoverflow") { $(this).attr("disabled", "disabled").siblings().removeAttr("disabled"); } });
Without enabling non-targeted elements:
$("option").each(function(){ if ($(this).val().toLowerCase() == "stackoverflow") { $(this).attr("disabled", "disabled"); } });
To collect a group of elements, we pass the selector to the jQuery function using…
jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is…
HTML:- [crayon-67afa20ae1b78293732654/] CSS:- [crayon-67afa20ae1b7d994729171/] JavaScript:- [crayon-67afa20ae1b7f539093350/]
Prevent users typing special characters in text box, textarea, etc. [crayon-67afa20ae1cca009165544/] ^[a-zA-Z]+$ Alphabets ^[a-zA-Z\s]+$ Alphabets…
Any interactive web site has form input - a place where the users input different…
[crayon-67afa20ae1fba471229866/]