Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all elements with a certain attribute value in jquery

Tags:

jquery

I need to find all elements that has a special attribute value.

Here is the div I need to find (I have many of them..)

<div imageId='imageN'>... 

I simply need to loop through the divs which have imageId='imageN'

like image 820
Pabuc Avatar asked Feb 10 '11 14:02

Pabuc


People also ask

How do you find the element based on a data attribute value?

Answer: Use the CSS Attribute Selector You can use the CSS attribute selectors to find an HTML element based on its data-attribute value using jQuery. The attribute selectors provide a very powerful way to select elements.

How do you select an element with specific attributes?

The [attribute^="value"] selector is used to select elements with the specified attribute, whose value starts with the specified value. The following example selects all elements with a class attribute value that starts with "top": Note: The value does not have to be a whole word!

How get value from data attribute in jQuery?

You can use this jquery data() syntax for get data-id attribute value. $("selector"). data("textval"); You can use this jquery data() syntax for get data-textval attribute value.

What jQuery syntax selects HTML elements by a specific attribute and its value?

The [attribute|=value] selector selects each element with a specified attribute, with a value equal to a specified string (like "en") or starting with that string followed by a hyphen (like "en-us").


1 Answers

Although it doesn't precisely answer the question, I landed here when searching for a way to get the collection of elements (potentially different tag names) that simply had a given attribute name (without filtering by attribute value). I found that the following worked well for me:

$("*[attr-name]") 

Hope that helps somebody who happens to land on this page looking for the same thing that I was :).

Update: It appears that the asterisk is not required, i.e. based on some basic tests, the following seems to be equivalent to the above (thanks to Matt for pointing this out):

$("[attr-name]") 
like image 189
sammy34 Avatar answered Sep 29 '22 20:09

sammy34