23

First of all; example of HTML code:

<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 that class contains "grap" word somewhere is class name (so in this case first three divs). How can i achieve that ?

This is not working: http://api.jquery.com/attribute-contains-word-selector/

pigrammer
  • 2,603
  • 1
  • 11
  • 24
born2fr4g
  • 1,290
  • 4
  • 27
  • 38
  • 1
    The "attribute contains word" selector is not working because your "word" is not delimited by whitespace. – FtDRbwLXw6 Sep 11 '11 at 13:12
  • this question was asked before, see http://stackoverflow.com/questions/4106958/jquery-select-only-a-class-containing-a-string – shiying yu Sep 11 '11 at 13:16

5 Answers5

53

Use the attribute contains selector:

$('div[class*="grap"]')
Andrei
  • 55,890
  • 9
  • 87
  • 108
4

Depending on your particular use-case, you could try the attribute prefix selector:

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

It seems from your sample data that "grap" is always prefixing the class, so if that's the case this may be what you want. If "grap" can be anywhere in the class instead of just at the beginning, use *= instead.

FtDRbwLXw6
  • 27,774
  • 13
  • 70
  • 107
2

Use the "attribute contains" selector: http://api.jquery.com/attribute-contains-selector/
The "attribute contains word" selector won't work because, according to the docs, it finds elements where the specified attribute contains the given word delimited by spaces.

Abraham
  • 20,316
  • 7
  • 33
  • 39
1

You want to use the "Attribute contains selector", not the "Attribute contains WORD selector", which is what you linked to.

maxedison
  • 17,243
  • 14
  • 67
  • 114
1

What you're looking for is the "attribute contains selector"

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

Kemal Fadillah
  • 9,760
  • 3
  • 45
  • 63