tags:

views:

49

answers:

3

I have a series of records with ids assigned to them. I.e.

record1, record2, record3 etc

I'm trying to get the id of the link being clicked using:-

$("a.removeTier").live('click', function() {
    var tier = $(this).attr('id').match('/\d+$/');
    alert(tier);
});

The variable tier, should only contain the numeric value within the string. Currently, I'm getting null.

Any ideas?

+1  A: 

Using a blunter object:

$(this).attr('id').substring("6","7")

As noted in comments, this only works for one-digit numbers. So do this instead:

$(this).attr('id').replace("record", "") 
morgancodes
I'm not sure this will work as the number could get very large.
Of course, you don't know how many digits it will be. In that case, try this. $(this).attr('id').replace("record", "")
morgancodes
And hey, why the downvote? This is a reasonable solution.
morgancodes
Maybe the downvote was because of the crazy typo in my call to subString.
morgancodes
@morgancodes: *substring()* is also all lowercase. I've fixed it for you. +1 for your *replace()* example, which is a good alternative.
Andy E
Thank you Andy E.
morgancodes
+5  A: 

Why not just use slice() or substring()?

 var tier = this.id.slice(6);
 // -> 1, 2, 3... 11... 123, etc

Example - http://jsfiddle.net/TmBQ8/

PS, you're getting null at the moment because you're passing a string argument to match, instead of a regular expression. Remove the quotes, e.g. match(/\d+$/). Also note in my example, I skipped using a jQuery wrapper and attr() since it's the long way around and not as efficient as direct property access.

Andy E
+1  A: 

If your id always has "record" in front of it then...

$("a.removeTier").live('click', function() {
    var tier = $(this).attr('id').subString("6");
    alert(tier);
});

If you change the word "record" to something else just change the 6 the the position of the first number.

Capt Otis