Given a list of HTML inputs, textareas, and selects, I would like to check and see if any have a name attribute equal to a given string, and if any do, to add a class to it.
For example:
if($('textarea, input, select').attr("name") == "this_name"){
$(this).addClass("myClass");
}
I'm just not sure what I', doing wrong here, but it doesn't seem to be working.
try:
$("textarea[name='this_name'], input[name='this_name'], select[name='this_name']").addClass("myClass");
Look to use CSS3 selectors within jQuery
What you are looking for is:
$('textarea, input, select').filter('[name="this_name"]').addClass("myClass");
because your .attr()
will look for the name
attribute of the first matched element (see the documentation on attr)
Something like:
$("textarea[name='this_name']").addClass("myClass");
See: http://api.jquery.com/attribute-equals-selector/
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