jesse
jesse

Reputation:

How to get to know which anchor link being clicked using javascript?

Sorry I am new to JavaScript.

Says that a.html has two same anchor links (<a href="b.html">b</a>). The anchor links are uneditable but we could add JavaScript to the a.html.

Except for dynamic adding onclick event, any other ways could get to know which anchor link being clicked?

Upvotes: 1

Views: 5914

Answers (4)

Manan
Manan

Reputation: 257

you can also try this one

 <script type="text/javascript">
function testClick() {
    alert(this.id);
}
 </script>

  <a href="#" id="test" onclick="testclick();">Click</a>

Upvotes: 1

ketan
ketan

Reputation: 19351

You can use javascript on anchor tag call testClick like:

<script type="text/javascript">
    function testClick(id) {
        alert(id);
    }
</script>

<a href="#" id="test" onclick="testclick(this.id);">Click</a>

Upvotes: 1

Sebastian
Sebastian

Reputation: 1531

You can use jQuery. Add the click event to the anchors and show the id using an alert:

$(document).ready(function(){
$("#anchor1").click(function(){
    alert($(this).attr('id'))
    });
$("#anchor2").click(function(){
    alert($(this).attr('id'))
    });
});

Upvotes: 1

Sampson
Sampson

Reputation: 268512

The only way to check is, as you suggested, through the click event.

Upvotes: 0

Related Questions