62

I have a button on my page with a class of comment_like and an ID like comment_like_123456 but the numbers at the end are variable; could be 1 to 1000000.

When this button is clicked, I need to grab the end number so I can run tasks on other elements with the same suffix.

Is there an easy way of grabbing this number in jQuery?

$('.comment_like').click(function() {
 var element_id = $(this).attr('id');
 // grab number from element ID
 // do stuff with that number
});
asked Feb 14, 2012 at 19:24
0

8 Answers 8

119

You can get it like this:

var suffix = 'comment_like_123456'.match(/\d+/); // 123456

With respect to button:

$('.comment_like').click(function(){
 var suffix = this.id.match(/\d+/); // 123456
});
answered Feb 14, 2012 at 19:27
Sign up to request clarification or add additional context in comments.

2 Comments

what if var suffix= '(123) 131-2312';
@Swaraj In that case, if you want just the last series of digits, you can use this regular expression where the dollar sign means that the pattern must be at the end of the string: /\d+$/
19

You can try this. it will extract all number from any type of string.

var suffix = 'comment_like_6846511';
alert(suffix.replace(/[^0-9]/g,''));

DEMO

answered Mar 28, 2018 at 10:30

Comments

15

In your click handler:

var number = $(this).attr('id').split('_').pop();
answered Feb 14, 2012 at 19:29

1 Comment

As long as number always comes at end with an underscore before. Tightly coupled as long as html or requirements do not change :)
4

This is a task for plain-old regular expressions, it has nothing to do with jQuery:

"comment_like_123456".match(/\d+/)
=> ["123456"]
answered Feb 14, 2012 at 19:28

Comments

4

http://jsfiddle.net/hj2nJ/

var x = 'comment_like_6846511';
var y = '';
for (i = 0; i < x.length; i++)
{
 if ("" + parseInt(x[i]) != "NaN") //if the character is a number
 y = y + x[i];
}
document.write(y);
answered Feb 14, 2012 at 19:32

Comments

2

jQuery is not a magic bullet. Use Javascript!

var temp = "comment_like_123456".split("_")
alert(temp[2])
answered Feb 14, 2012 at 19:27

Comments

2

This would be the easiest way to grab number from a string using jquery.

$(function(){
	 $('.comment_like').click(function() {
 var element_id = $(this).attr('id');
 var number = element_id.match(/\d+/);
 
 alert(number);
 });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class='comment_like' id='comment_like_123456'>Click Me To Get a number from string</button>

answered Jun 5, 2020 at 21:28

Comments

1

Just get the id and run it through a regex.

$(mybutton).click(function() {
 var num = parseInt(/^.*\_(\d+)$/.exec(this.id)[1])
});
answered Feb 14, 2012 at 19:28

2 Comments

what's [1] here after exec?
@user938363 It gets the first capturing group in the matched string (i.e. everything between the first set of parenthesis, what was matched by the \d+ - a number)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.