Sunday 6 September 2009

Jquery $ and how to declare variables

Today, I got little bit of time and want to share with you small things I learn in JQuery.
I know, most of the people who are new to Jquery don't know what is $ in JQuery.
So, $ is the symbol which is the initialization of Jquery object. You can pass selector, function, string etc to execute or implement some functionality.
So, $("#idOfElement") or JQuery("#idOfElement") both are same.

I saw many people are declaring the Jquery variables as shown below.
var currentObject = null;
currentObject = $("#someID");

So, I don't say this is the best practice to use it. Instead do the below.
var $currentObject = null;
$currentObject = $("#someID");
And if you want to use it, you can directly use it as shown below.
$currentObject.val() or any other function supported for the current object.

So, what the advantage here is, for easily identifying the Jquery variables. If you are working on a project where it needs lot of Jquery coding, this helps!

No comments:

Post a Comment