kralco626
kralco626

Reputation: 8624

Jquery select all objects with a particular attribute

I want to select all elements on my page that have a particular attribute.

For example, I have an attribute called "tip" I add tip="...whatever..." to a lot of my html tags. I would like to be able to select all html tags that have this attribute.

NOT all html tags that have that attribute equal to a particular value. Just have the attribute AT ALL; REGARDLESS of value.

I apologize for my confusing wording and inadiquite vocabulary in my question. I have now updated the question to use proper wording.

Thanks!

Upvotes: 0

Views: 320

Answers (3)

Sarfraz
Sarfraz

Reputation: 382696

Update:


You can do like this:

$('[tip="something"]')

You can replace something with whatever your string is.


You can use :contains filter selector like this:

$('p:contains("tip")').color('red', 'green');

That for example will select all paragraphs that have tip keyword in their value/html.

Upvotes: 1

jyoseph
jyoseph

Reputation: 5455

If they are all attributes - e.g. tip="foo" then you can use the Attribute Equals Selector as shown below:

jQuery('[attribute="value"]')

So in your case:

$('div[tip="foo"]')

Demo Here

Upvotes: 0

charliegriefer
charliegriefer

Reputation: 3382

$( '[tip]' )

attributeHas selector

"Description: Selects elements that have the specified attribute, with any value."

See http://api.jquery.com/has-attribute-selector/

Upvotes: 2

Related Questions