document.getElementById On All Browsers – Cross browser getElementById

Here is a little JavaScript function that gets an object according to it’s id and it’s cross browser.

/*********************************************************************
   * Get an object, this function is cross browser
   * Usage:
   * var object = get_object(element_id);
   * @Author Hamid Alipour http://blog.code-head.com/
  **/
  function get_object(id) {
   var object = null;
   if (document.layers) {
    object = document.layers[id];
   } else if (document.all) {
    object = document.all[id];
   } else if (document.getElementById) {
    object = document.getElementById(id);
   }
   return object;
  }
  /*********************************************************************/
document.getElementById On All Browsers – Cross browser getElementById
Filed under: JavaScript,Web Browsers,Web Development   Posted by: CodingRecipes

4 Comments »

  1.  

    is this better than jQuery selectors?

    Comment

     
  2. Codehead:
     

    Nope, it’s not, I recommend jQuery, this is for circumstances where it must be done this way…

    Comment

     
  3.  

    ok, thanks

    Comment

     
  4. n00b81:
     

    Thanks man, just what I needed.

    Comment

     

RSS feed for comments on this post. TrackBack URL

Leave a comment