Reputation: 15950
I want to extract number from 'id' of <a> tag
<a id="t-1">Details - 1</a>
<a id="t-2">Details - 2</a>
<a id="t-3">Details - 3</a>
when I click on any of the anchor tag, I have to show which number of link it is
Say when I click on <a> tag with id = "t-1", then I need to alert 1 on screen.
How can I do using jQuery?
Upvotes: 0
Views: 63
Reputation: 5198
$(document).ready(function ()
{
$("a").click(function (e)
{
e.preventDefault();
id = $(this).attr("id");
arr = id.split("-");
alert(arr[1]);
});
});
I haven't chained the code so that you can see what's going on.
Upvotes: 1
Reputation: 6983
You can set up a very simple click event for that:
$("a").click(function(e) {
e.preventDefault();
var id = $(this).attr("id").split("-")[1];
alert(id);
});
Here's a working example: http://jsfiddle.net/aAYvd/1/
Upvotes: 0
Reputation: 4696
Try this out
var id = $("#t-1").attr('id');
alert(id.subString(1,2);
Upvotes: 0