John S
John S

Reputation: 8331

Using jquery to find elements with data-attribute that has a specific value?

Is there a basic way via jquery to find all elements that have a data-attribute with a specific value and return those elements as an array?

i.e. <span data-productID="7">My product</span>

Upvotes: 0

Views: 923

Answers (1)

user1726343
user1726343

Reputation:

The syntax for selecting elements with a specific attribute value is:

[attr_name = attr_value]

In your specific case, this would be:

$('span[data-product="7"]');

This returns a jQuery collection (not an Array), but it can be accessed much like an array using numerical indices. If you really need an array (perhaps in order to use Array prototype methods), you can use:

$('span[data-product="7"]').makeArray();

Upvotes: 2

Related Questions