How to get elements of specific class starting with a given string?
You can also try:
$(".etape").click(function () {
var theClass = $(this).attr("class").match(/btn[\w-]*\b/);
console.log(theClass);
});
Uses match instead of grep...
// Only select input which have class
$('input[class]').click(function(){
var myClass;
// classNames will contain all applied classes
var classNames = $(this).attr('class').split(/\s+/);
// iterate over all class
$.each(classNames, function(index, item) {
// Find class that starts with btn-
if(item.indexOf("btn-") == 0){
// Store it
myClass = item;
}
});
});
Live Demo
You can use Regular Expression or split
the class name.
$(".etape").click(function(){
var classes = $.grep(this.className.split(" "), function(v, i){
return v.indexOf('btn') === 0;
}).join();
});
http://jsfiddle.net/LQPh6/