Web 2.0 Training
 

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);
}


Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • BlinkList
  • Furl
  • IndianPad

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 *

*
*

  Wordpress Themes Protected By Wp Spam Blocker