Home | Php | JavaScript | SQL | HTML | Server Admin | Tools
JavaScript
Welcome
History

Change Cell Color on Mouseover
Change Background Color
Break out of Frames
Change Cursor
Limit Input Text
Get Object By ID
Up/Down Buttons (Spinner)
Other sites
Image Editor
Top 10 web hosting reviews

Get object by ID

If you've been working with Javascript at all, you've come to use the getElementById method. While this function is a good one for simply getting an object, it's not compatible with all browsers. Today I'm going to show you how to work towards cross-browser compatibility in your JavaScipt code. Just use the following function instead of getElementById().

Cross-browser compatible getElementById() Function

Example Code
function getElementByIdCompatible (the_id) {
if (typeof the_id != 'string') {
return the_id;
}

if (typeof document.getElementById != 'undefined') {
return document.getElementById(the_id);
} else if (typeof document.all != 'undefined') {
return document.all[the_id];
} else if (typeof document.layers != 'undefined') {
return document.layers[the_id];
} else {
return null;
}
}



Link back to this page

Copy and paste this HTML code into your page to link back to this very page:
     

Or just link to the WebCodingTech site:
     

And your link will look like this:
      Inspired by stuff found at www.webcodingtech.com.

Thanks!





Copyright © 2005 WebCodingTech.com Our URL is: http://www.webcodingtech.com