Javascript: How to get an Object’s ID

In Javascript, one of the most common things you do is document.getElementById(‘someid’).  But what if you got the Element from an Event and you want to use that Object with the Prototype Javascript Library or some other library that relies on using the ID? 

In that case, you can’t just tack on the Prototype functions like:  obj.addClassname(‘someclass’);  You have to use an object that is extended with the Prototype or other library’s magic. 

In Prototype, the easiest way to do that is to get the Object’s ID and use the  $(‘someid’).someFunction(); methodology.  Getting the ID is really simple, though I spent 20 minutes trying to find it on Google.  Here’s how:

  var ID = object.id;

There you go!

Byron

7 Responses to Javascript: How to get an Object’s ID

  1. Rich March 6, 2009 at 8:01 pm #

    Ahhhhh…thank gawd that’s over! So simple but it took sooooo long to find the solution.

  2. Byron Bennett March 7, 2009 at 10:15 pm #

    I hear you, man. Why do the little things eat up so danged much time???

  3. c4mp3r April 3, 2009 at 6:56 pm #

    how can i do this for the Internet Explorer….object.id for me only in Firefox,Opera,Netscape…but no IE

  4. Byron Bennett April 3, 2009 at 9:37 pm #

    C4mp3r,
    Uggg…I don’t know. I’ll need to try this at work. I don’t have IE here at home.

    BB

  5. Martin May 28, 2010 at 12:00 pm #

    Valuable information. Thanks to you I don’t have to trawl the web like you did for 20mins. Cheers!

  6. satish September 15, 2010 at 6:02 am #

    i find statement like Object[“id of a html control”]. what it means.

  7. strony www tworzenie September 17, 2010 at 3:49 am #

    Great info ! It’s working – remember to update your browser (crappy ie 6)

Powered by WordPress. Designed by Woo Themes