Ofeargall
Ofeargall

Reputation: 5670

How do I find all elements with a specific 'name' attribute using jQuery

I want to find all the elements on my page that have a 'name' attribute containing 'x' or 'y' and add a certain class to those elements.

So, the logic I'm looking to apply is

If anything has 'x' or 'y' in the name attribute then add .yepItHasXorY

It seems like a simple thing. I understand how to search based on an ID or Class but not the name attribute...

Upvotes: 2

Views: 576

Answers (1)

Nick Craver
Nick Craver

Reputation: 630359

You can use an attribute-contains selector, like this:

$("[name*=x], [name*=y]").addClass('yepItHasXorY');

Though, I would add an element type, on there or maybe :input to you're only looking at elements you care about (otherwise it's a much more expensive selector).

Upvotes: 1

Related Questions