A Cross-Browser Compatible Replacement For getElementById()

I have had problems many times writing JavaScript which is compatible across the major web browsers. Now, with AJAX and JSON becoming commonplace, this problem is come across even more often. So, how do you solve it? Well, I created a JavaScript function which works in place of getElementById() and I call it getObj().

The getObj() function basically uses Internet Explorer compatible code AND Mozilla compatible code along with tertiary logic to automatically determine the appropriate action for selecting a DOM object by it's ID. Here's the code:

// Browser independent replacement for document.getElementById()
function getObj(name) {
var object = document.getElementById
? document.getElementById(name)
: document.all
? document.all[name]
: document.layers[name];
return object ;


Popular Posts