tags:

views:

82

answers:

2

Hi there,

I have the folowing html markup.

<DIV class="bubble bubble_white">
  <DIV class=bubble_large></DIV>
</DIV>
<DIV class="bubble bubble_black">
  <DIV class=bubble_large></DIV>
</DIV>

I want to select the classes bubble bubble_white and bubble bubble_black.

I was thinking about the code underneath but didn't work.

$(".bubble.[class^=bubble_]")

Any ideas?

+2  A: 

Try this:

$(".bubble[class*=bubble_]")
PetersenDidIt
Thanks fine!!!!!!
Martijn
Note that this will also match something like “foobubble_bar”.
Gumbo
A: 

The [attr^=val] selector is comparing the whole attribute value. So your attribute value must begin with bubble_ to be selected. For a whitespace-separated list, you could use the [attr|=val] selector:

$(".bubble[class|=bubble_white], .bubble[class|=bubble_black]")

Or you do the filtering on your own:

$(".bubble").filter("[class|=bubble_white], [class|=bubble_black]")

Or:

$(".bubble").filter(function() {
    var $this = $(this);
    return $this.hasClass("bubble_white") || $this.hasClass("bubble_black");
})

Or:

$(".bubble").filter(function() {
    return /(?:^|\s+)bubble_/.test(this.className);
})
Gumbo
Works as well. Thanks!
Martijn