Reputation: 54113
I have elements. I know for sure they will have a class 'fc-id#' where '#' is a number.
I need to somehow get that number. This will always be the first class of the element.
Is there some way to get the number at the end?
Thanks
Upvotes: 2
Views: 2078
Reputation: 9370
$("[class^=fc-id]").each(function(){
var num = this.className.split(' ')[0].match(/fc-id(\d)/)[1];
});
Upvotes: 4
Reputation: 3556
$("[class^=fc-id]").each(function(){
// Everything after the first 5 characters (fc-id)
alert($(this).attr("class").substring(5));
})
Upvotes: 0
Reputation: 206048
use .split()
method like
var idNum = $(this).attr('class').split('id')[1]; // #
or if you need to retrieve the first class do like:
var idNum = $(this).attr('class').split(' ')[0].split('id')[1]; // #
Upvotes: 0