Select a div (or other element) in which the class contains "grap" (for example, another specified word)

First of all; HTML code example:

<div class"grap1"> some conetent</div> <div class"grap2"> some conetent</div> <div class"grap3"> some conetent</div> <div class"blabla">some content></div> 

And now I want to select divs in which the class contains the word grap, somewhere this is the name of the class (so in this case the first three divs). How can i achieve this?

This does not work: http://api.jquery.com/attribute-contains-word-selector/

+8
jquery contains class attributes word
source share
5 answers

Use the attribute containing the selector :

 $('div[class*="grap"]') 
+25
source share

Depending on your specific use case, you might try to select an attribute prefix:

 var $grapDivs = $('div[class|="grap"]'); 

According to your sample data, it seems that β€œgrap” is always a class prefix, so if possible, this might be what you want. If grap can be anywhere in the class, not just at the beginning, use * = instead.

+4
source share

Use the "attribute contains" selector: http://api.jquery.com/attribute-contains-selector/
The "attribute contains the word" selector will not work because, according to the docs, it finds elements where the specified attribute contains the given word, limited to spaces.

+2
source share

You want to use the " Attribute Contains Selector " rather than the "Attribute Contains WORD Selector" with which you are associated.

+1
source share

What you are looking for is an "attribute contains selector"

See an example here http://jsfiddle.net/CsvUY/

+1
source share

All Articles