jmasterx
jmasterx

Reputation: 54113

Parse classname for number at end?

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

Answers (3)

Anujith
Anujith

Reputation: 9370

$("[class^=fc-id]").each(function(){
      var num = this.className.split(' ')[0].match(/fc-id(\d)/)[1];
});

Sample

Upvotes: 4

BinaryTox1n
BinaryTox1n

Reputation: 3556

$("[class^=fc-id]").each(function(){
    // Everything after the first 5 characters (fc-id)
    alert($(this).attr("class").substring(5)); 
})

Upvotes: 0

Roko C. Buljan
Roko C. Buljan

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

Related Questions