Thursday, March 01, 2007

Javascript document.all vs document.getElementById

Recently I was playing with a project using ajax and php.
I wanted that one select box "onchange" event would call an ajaxian function.
Then it would go to php and updated the information in the screen. Clean. fast and easy. Yeah!

Then I tried to pass the select box to the function using "this".
OOP rules.

something like onchange="ajax_set_client_id(this)"

here in this function i tried to get another select box present in the form and call another similiar function.

i tried using this piece of code i found in the net.

if (document.all) {
element = document.all[id];
else {
element = document.getElementById(id);
}

Please dont use it. Please stick to document.getElementById(id);
Simon Willison says basically it it usefull and old school.
all the browsers since 1998.. almost 10 years ago, support the good old document.getElementById(id);



i found later an article here.
http://simonwillison.net/2003/Aug/11/documentAll/