fatiDev fatiDev - 4 months ago 9
jQuery Question

How to get elements of specific class starting with a given string?

I have a list of elements that have multiple classes, for example:

<input class="etape btn-info others">
<input class="etape btn-inverse others">
<input class="etape btn-danger others">


How to write jQuery-code that will allow me the following...

$(".etape").click(function(){
$(this).get("the class that starts with btn-")
// in order to store this value in a variable to reuse it later
});

Answer

You can use Regular Expression or split the class name.

$(".etape").click(function(){
   var get = $.grep(this.className.split(" "), function(v, i){
       return v.indexOf('btn') === 0;
   }).join();
});

http://jsfiddle.net/LQPh6/

Comments