Buy me a Beer
Web 2.0 Training
 
headermask image

Random Header :: Unpredictably Exciting

A simple shortcut in Javascript !

document.getElementById(’id_of_element’).value

How many of you tried to use this JavaScript’s case sensitive way to access an element ? Have you ever wished for a smaller code to access the element ? If yes… I am here to grant your wish with this simple function.

Just add the following line on the top of your JavaScript code:

function $(o){return document.getElementById(o);}

Done ? Thats it… :) what were you expecting more steps ?

Well after you include it on the top, this is how you access a div with id=”idname”

$(”idname”).value;

Pretty neat huh ? This is what the magic function does:


function $(obj) {
           return document.getElementById(obj);
}


If you liked my post, feel free to subscribe to my rss feeds

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*