I'm sure this is painfully simple but I just can seem to find it.
I need to get a selection of textboxes from their value. I don't need the value, I need the elements. I want something like:
$(".ProductCode [value:'hideme']").hide();
I end up with
unrecognized expression: [value:'hideme']
btw,
$(".ProductCode").each(function() { if ($(this).val() == 'hideme') $(this).hide(); });
Is working but it doesn't seem very clean.
Use the jQuery: selected selector in combination with val () method to find the selected option value in a drop-down list.
We can select text or we can also find the position of a text in a drop down list using option:selected attribute or by using val() method in jQuery. By using val() method : The val() method is an inbuilt method in jQuery which is used to return or set the value of attributes for the selected elements.
var variableValue = $("selector option: selected"). text(); The “option: selected” attribute is used to select specific content in the option tag.
val () method in jQuery: This method is used to get the values of form elements or set the value of attribute used for the selected elements. Syntax: $(selector). val ();
Use the attribute equals selector of jQuery
$(".ProductCode[value='hideme']").hide();
To be more precise, you could also use the multiple attribute selector:
$("input[class='ProductCode'][value='hideme']").hide();
The difference between the two is that the first selects all elements with a certain class and value. The second only selects all INPUTs with a certain class and value.
This selectors will select all of the applicable elements. So that hide()
function will hide all of the elements. So there is no need to "manually" iterate through the selected elements with each()
or other things.. hide()
automatically does that for you.
Here is a live example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With