William
William

Reputation: 1030

How to get a number from a class name in jquery?

I tried several solutions but the code still doesn't work...

Here is what I got so far:

$('.next-arrow').click(function (){
    var num = $(this).closest('[class^=step-]').match(/\d+/)[0];
    console.log(num);
});

If I run it without the .match() it works fine and tells me that I'm at "step-1" on console log, but adding .match() doesn't return anything.

What I'm trying to do here is to check where I'm at "step-1, step-2,..." and assign that number to hide the currently step and show the next step.

Upvotes: 1

Views: 1795

Answers (1)

smnbbrv
smnbbrv

Reputation: 24541

You just forgot to take a class of the found element instead of the element itself:

$('.next-arrow').click(function (){
    var num = $(this).closest('[class^=step-]').attr("class").match(/\d+/)[0];
    console.log(num);
});

Upvotes: 1

Related Questions