Alex
Alex

Reputation: 68492

jQuery - match element that has a class that starts with a certain string

I have a few links that look like this:

<a href="#" class="somelink rotate-90"> ... </a>

How can I bind a function to all elements that have a class that starts with "rotate-" ?

Upvotes: 7

Views: 5535

Answers (1)

Sarfraz
Sarfraz

Reputation: 382696

You can use starts with selector like this:

$('a[class^="rotate-"]')

Description: Selects elements that have the specified attribute with a value beginning exactly with a given string.

So your code should be:

$('a[class^="rotate-"]').click(function(){
  // do stuff
});

Note: If you want to find out elements that contain certain text anywhere in their attributes, you should do this instead:

$('a[class*="rotate-"]').click(function(){
  // do stuff
});

Upvotes: 10

Related Questions