the $ function

Thursday 16th August 2007 10:55 PM

TAGS: javascript

Nothing ground breaking, but it is amazing how annoying complicated and messy it is to refer to an element using

document.getElementById("element");

This usually always means that you have to assign it to another variable to make it convenient enough to deal with. Most frameworks use the $ function, that is - you can do the exact same as above, by using

$('element');

Suppose you don't need an entire framework, but want to retain this convenient function - it's incredibly easy to replicate for your own JS repository.

function $(element)
{

  return document.getElementById(element);

}

 Just a small little tip for those of you who may not be aware of this great idea first introduced by the prototype library, if you code javascript much at all, you'll learn to love this function!

Comments on this article:


I love feedback and comments, be the first!

Add Comment:


Make a Comment

*Nb, all comments are moderated to prevent spam or inappropriate content.








netforge logo
netforge provides high quality and friendly website design services to business. We're Australian based and reliable... (find out more).