Jump to content

how to use javascript to check which element was clicked using its id ?


jd2007

Recommended Posts

Why not just include it with the onClick();?

 

Like:

<a href="backifnojs.html" onclick="getID(link1)" id="link1">link 1</a>
<a href="backifnojs2.html" onclick="getID(link2)" id="link2">link 2</a>

 

Thats just how I would do it.

Link to comment
Share on other sites

You should not be placing any Javascript directly into your HTML.  If you read up on the proper way to use the JS event model you can determine the DOM node that caused the event without passing anything to the event handler.

 

I don't feel like typing up an extended example right now, but this should get you started:

http://www.quirksmode.org/js/introevents.html

 

There are two basic event models you should support, the IE model and the DOM Level 2 model.  At this stage of the game I think you can safely ignore the Level 0 model.

Link to comment
Share on other sites

The latest 'standards' event model sucks! you lose the use of the 'this' keyword and you have to double code for ie and everyone else.  I hate to say it but it's a big step backwards. 

 

He would have to write a bunch of ugly 'standards' code to replace:

<a href="backifnojs.html" onclick="getID(this.id)" id="link1">link 1</a>
<a href="backifnojs2.html" onclick="getID(this.id)" id="link2">link 2</a>

--the id will be passed as the parameter to getID, use it in a "document.getElementById()" statement

Link to comment
Share on other sites

The latest 'standards' event model sucks! you lose the use of the 'this' keyword and you have to double code for ie and everyone else.  I hate to say it but it's a big step backwards.

 

That's why I wrapped it up in my own Javascript events interface that creates a single client interface despite the browser being used.  All of my Javascript is much cleaner and more concise.

 

function someHandler(oEvent){
  alert(oEvent.node.value);
}

Events.addEvent( document, "load", function(oEvent){
  // I can add events two ways, with the element or with it's ID
  var el = document.getElementById("some_textbox");
  Events.addEvent( el, "change", someHandler );

  Events.addEvent( "other_textbox", "change", someHandler );
});

Link to comment
Share on other sites

Once you have the event object, either passed to the function with the DOM L2 model or through the global window.event object in IE, you can access the node that triggered the event.

 

  function handler(oEvent){
    oEvent = oEvent || window.event;  // Get the proper event object
    var oNode = oEvent.target || oEvent.srcElement; // Get the proper node object

    oNode.value = "Hello, World!";
    // Or if it's a list box
    alert(oNode.options[oNode.selectedIndex].value);
  }

 

If you use the event models correctly, you don't need the this keyword.  In addition, the node object will have all the properties of the this keyword in addition to the DOM methods as well.  Your statement about having to handle multiple models being a pain is correct, but that doesn't absolve you from being a good programmer and working with what is the current standard.

 

My suggestion:  Learn how to use the event models and get your Javascript out of your HTML; it just makes it ugly.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.