1

In my html page I have a many links that their ID can change all the time it looks something like that (I have around 140 links):

 <a id=33 onclick="javascriptfunction()"
           href="33">link1</a>
 <a id=37 onclick="javascriptfunction()"
           href="37">link2</a>
 <a id=113 onclick="javascriptfunction()"
           href="113">link3</a>
 <a id=3 onclick="javascriptfunction()"
           href="3">link4</a>
          .....
          .....
          .....

Client can randomly select one of them and I want to perform operation in javascriptfunction() that is of course in JavaScript based on their element ID.

Is there a way to get in JavaScript(no jQuery) the link element ID which was clicked?

5
  • 3
    First of all, you need these around your ID ". Next: an ID cannot start with a number! Commented Jul 3, 2013 at 13:06
  • 1
    @BramVanroy -- HTML5, ID's can start with anything now :) Commented Jul 3, 2013 at 13:09
  • Yeah forgot to add those :) Commented Jul 3, 2013 at 13:14
  • onclick="alert this.id" @Bram 1) wrong - it's html not xml 2) wrong too, html5 assumed. Commented Jul 3, 2013 at 13:16
  • @Everyone: I was indeed wrong in the first part. It seems good practice to add 'em though. Also, the post was not tagged HTML5 so I assumed an older format. Commented Jul 3, 2013 at 13:47

5 Answers 5

5

Something like this...

<a id=2 onclick="javascriptfunction(this)"
           href="#">link4</a>

// In javascript
function javascriptfunction(element){
    var id = element.id;
}
Sign up to request clarification or add additional context in comments.

Comments

3

You can try:

<a id=3 onclick="return javascriptfunction(this)" href="3">link4</a>

function javascriptfunction(ID){
   alert(ID.id);
   return false;
}

4 Comments

Maybe even javascriptfunction(this.id) ?
It won't work as you typed, you sent this, and this is anchor.
But...you're alerting the object of this, you mean ID.id
@DadoJerry not a problem, as long as it helps someone.
2
<script>
function javascriptfunction(id)
{
    alert("You clicked a tag with the id "+ id);
}
</script>
<a id=33 onclick="javascriptfunction(this.id)"/>

You could use this.

Comments

0

If you don't want to change all 140+ links in the HTML markup, you can also inject the following JavaScript code in the <head> section:

window.onload = function() {
    var anchors = document.getElementsByTagName("a");
    for (var i = 0; i < anchors.length; i++) {
        var anchor = anchors[i];
        var id = anchor.id;
        if (id && id.length > 0) {
            anchor.onclick = function() {
                RealClickHandler(this.id);
                return false;
            };
        }
    }
};

This will override the existing onclick of all the links, and call different function passing it the id. Sample function might look like this:

function RealClickHandler(id) {
    alert("anchor with id " + id + " was clicked");
}

Live test case.

Comments

-1

Maybe use jQuery! Here is example.

$("div").on('click', 'button' , function(obj){
    alert(obj.target.id);
});

2 Comments

The OP explicitly asks for a non-jQuery solution.
When is “use jQuery” not a valid answer to a JavaScript question? All the following apply here: 1) It is the entire answer, with no explanation of exactly how jQuery helps, 2) When the problem can be solved simply without jQuery, 3) When the question specifically asks for no javascript frameworks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.