9

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.

2
  • There's no need to use each(). When you select elements, you'll preform hide() on all the selected elements. I've edited my answer to include a jsFiddle example that hides 2 text boxes at once. Commented Aug 11, 2010 at 19:58
  • ok, not only did I have a : instead of an = but I had a space in there too. Thanks to both, I gave it to @peter-ajtai for the jsFiddle example. but +1 to both. Commented Aug 11, 2010 at 20:04

2 Answers 2

17

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.

Sign up to request clarification or add additional context in comments.

Comments

7

Try:

$(".ProductCode[value='hideme']").hide(); 

See Attribute Equals Selector in the jQuery docs for more details.

2 Comments

wow, I should have seen that. So I'm not getting the error anymore, but I'm not getting anything selected either. Is there a list of the attributes, or is it the HTML attributes specifically that jQuery is referring to?
Thanks Justin, I got it, the space was throwing me off as well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.