I-M-JM
I-M-JM

Reputation: 15950

extracting part of id from anchor tag

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

Answers (3)

Dormouse
Dormouse

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

rubiii
rubiii

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

shashi
shashi

Reputation: 4696

Try this out

var id = $("#t-1").attr('id');

alert(id.subString(1,2);

Upvotes: 0

Related Questions