Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Get a selection based on value

Tags:

jquery

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.

like image 321
Dan Williams Avatar asked Aug 11 '10 19:08

Dan Williams


People also ask

How to get select option value in jQuery?

Use the jQuery: selected selector in combination with val () method to find the selected option value in a drop-down list.

How to get selected option value and text in jQuery?

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.

How do you select options with value?

var variableValue = $("selector option: selected"). text(); The “option: selected” attribute is used to select specific content in the option tag.

How to get attribute value of dropdown in jQuery?

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 ();


1 Answers

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.

like image 168
Peter Ajtai Avatar answered Nov 15 '22 17:11

Peter Ajtai