Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery get certain class name of element which has several classes assigned

I need to read elements class name. I have elements like this:

<article class="active clrone moreclass">Article x</article>
<article class="active clrtwo moreclass">Article y</article>
<article class="active clrthree moreclass moreclass">Article z</article>
<article class="active clrone moreclass">Article xyza</article>

I need to parse out class name that starts with clr. So if second element was clicked then I would need to get clrtwo className.

like image 564
Primoz Rome Avatar asked Jan 11 '12 14:01

Primoz Rome


1 Answers

You can use a regular expression match on the class name of the clicked item to find the class that begins with "clr" like this:

$("article").click(function() {
    var matches = this.className.match(/\bclr[^\s]+\b/);
    if (matches) {
        // matches[0] is clrone or clrtwo, etc...
    }
});
like image 152
jfriend00 Avatar answered Nov 10 '22 08:11

jfriend00