javascript tricks: document.getElementById

When programming with the DOM the function document.getElementById function is frequently used, we can reduce the code making it a function with a name shorter and easier to remember:
gEle=function(id){return document.getElementById(id);}

Sample usage

In our html source code we insert a DIV element with id “gEleSamp”
<div id="gEleSamp" style="border:solid 1px #ccc;">sample div</div>
The result of our code:
sample div
We access to the object with gEle using their id and we change the font color to red:
<span onclick="gEle('gEleSamp').style.color='#f00'">Test </span>
Test the code pressing the following link:Test

Leave a Reply

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