2

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

jmasterx
  • 52,639
  • 96
  • 311
  • 557
  • 3
    What have you tried? Look at http://stackoverflow.com/questions/190253/jquery-selector-regular-expressions for a start. – sachleen Mar 13 '13 at 18:10
  • 2
    Like: `var cls = element.className.split(' ')[0]; var num = cls.match(/fc-id(\d)/)[1];`? – Chad Mar 13 '13 at 18:12

3 Answers3

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

Sample

Barmar
  • 741,623
  • 53
  • 500
  • 612
Anujith
  • 9,370
  • 6
  • 33
  • 48
0

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]; // #
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
0
$("[class^=fc-id]").each(function(){
    // Everything after the first 5 characters (fc-id)
    alert($(this).attr("class").substring(5)); 
})
BinaryTox1n
  • 3,486
  • 1
  • 36
  • 44