document.all vs document.getElementById

document.all() -  is a non-standard way of accessing DOM elements. It was introduced in Internet Explorer 4, because the W3C DOM hadn’t yet standardised a way of grabbing references to elements using their ID. By the time IE 5 came out, document.getElementById() had been standardised and as a result, IE 5 included support for it.
document.getElementById() -  is a standard and fully supported. Each element have a unique id on the document. It is supported by every Javascript supporting browser released since 1998.

Or you can use:

JavaScript

if(document.getElementById){  //DOM
  element = document.getElementById(id);
}
else if (document.all) {  //IE
  element = document.all[id];
}
else if (document.layers){  //Netscape < 6
  element = document.layers[id];
}

About Midhun Devasia

I'm a 26 year old Web Developer from Cochin, India.I had completed my B.Tech Computer Science and Engineering from SNGCE, Cochin. Now am working as a Technical Manager for a Web Development company at Cochin, India . I have 1 year experience in Java and 3 year experience in Web Technologies.
This entry was posted in Javascript and tagged , , . Bookmark the permalink.

2 Responses to document.all vs document.getElementById

  1. Sreejith Sreedharan says:

    why use document.getElementById ?????

    you can use $(“someid”) as in jquery and mootools

    • Midhun Devasia says:

      @Sreejith Sreedharan: You can use $() function instead of this, if you are using any Js framework in your web-application.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>