From the following markup.
<div id="my-div">
<a href="#" id="link-1">Somewhere</a>
<a href="#" id="link-2">Somewhere else</a>
</div>
What are some options, using jQuery selectors and JavaScript for grabbing the integer in the ids?
For example.
$("#my-div a").click(function(){
$(this).id // ... somehow grab n from "link-n"
alert(n);
});
From the following markup.
<div id="my-div">
<a href="#" id="link-1">Somewhere</a>
<a href="#" id="link-2">Somewhere else</a>
</div>
What are some options, using jQuery selectors and JavaScript for grabbing the integer in the ids?
For example.
$("#my-div a").click(function(){
$(this).id // ... somehow grab n from "link-n"
alert(n);
});
Share
Improve this question
asked Jan 8, 2010 at 19:41
jeromejerome
4,98713 gold badges55 silver badges75 bronze badges
10 Answers
Reset to default 4You could try:
var n = $(this).attr('id').match(/link-(\d+)/)[1];
This fetches the id
attribute, matches against the pattern link-(\d+)
(which means link-
followed by one or more digits), and then extracts the first subexpression match (the part in the parentheses \d+
), which should be the number you are looking for.
If you need to work with n
as an integer instead of a string, you should should use parseInt
, making sure to specify base 10:
var n = parseInt($(this).attr('id').match(/link-(\d+)/)[1], 10);
If your id
attributes are not guaranteed to begin with link-
followed by one or more digits, and you would like to catch this case instead of throwing an error, you should check the return value of match
:
var match = $(this).attr('id').match(/link-(\d+)/);
if (match) {
var n = parseInt(match[1], 10);
alert(n);
} else {
// do something else if they don't match
}
$(this).attr('id').replace('link-','')
As long as the preceding text always remains the same you can just use the substring method to get the number.
$(this).attr('id').substring(5)
$(this).attr('id').split('-')[1];
var id = $(this).attr('id'),
regex = /(\d+)/,
matched = id.match( regex );
if ( matched ) {
alert( matched[1] )
}
I usually do something like this:
$("#my-div a").click(function(){
var match;
if (match = $(this).attr('id').match(/link-(\d+)/)) {
var number = parseInt(match[1],10);
alert(number);
}
});
Using a regex would be your best option, for instance:
// id contains '1' for id="link-1"
var id = parseInt(this.id.replace(/[^\d]/g, ''), 10);
If you know that all your ids are prefixed by "link-", you can simply get the substring of the id:
$("#my-div a").click(function(){
alert(this.id.substr(5));
});
This should be the simplest way:
var id = this.id.replace(/[^\d]/g,'')*1;
It returns any digits from the ID attribute as a number
(*1
does the conversion, similar to parseInt
). In your example:
$("#my-div a").click(function(){
var n = this.id.replace(/[^\d]/g,'')*1;
alert(n); // alerts any number in the ID attribute
alert(typeof n) // alerts 'number' (not 'string')
});
You can use a regular expression to parse out the number:
var match = /link-(\d+)/.exec($(this).attr('id'));
var num = match[1];