Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
155
rated 0 times [  162] [ 7]  / answers: 1 / hits: 54937  / 14 Years ago, thu, july 29, 2010, 12:00:00

Possible Duplicates:

Why would a javascript variable start with a dollar sign?

JQuery : What is the difference between “var test” and “var $test”






What is the difference between this two ways of initializing variables?



var $val = 'something'

OR

var val = 'something'


as I see they are the same thing.



Maybe in this case $ is only the part of name in variable?
(it will become a meaningless question in that case:/)



Thanks


More From » jquery

 Answers
23

The $ in the variable name is only part of the name, but the convention is to use it to start variable names when the variable represents a jQuery object.



var $myHeaderDiv = $('#header');
var myHeaderDiv = document.getElementById('header');


Now later in your code, you know the $myHeaderDiv is already a jQuery object, so you can call jQuery functions:



$myHeaderDiv.fade();


To get from the DOM-variable to the jQuery variable:



var $myHeaderDiv = jQuery(myHeaderDiv); //assign to another variable
jQuery(myHeaderDiv).fade(); //use directly

//or, as the $ is aliased to the jQuery object if you don't specify otherwise:
var $myHeaderDiv = jQuery(myHeaderDiv); //assign
$(myHeaderDiv).fade(); //use


To get from the jQuery variable to the DOM-variable.



var myHeaderDiv = $myHeaderDiv.get(0);

[#96084] Monday, July 26, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lucianom

Total Points: 601
Total Questions: 98
Total Answers: 109

Location: Kenya
Member since Fri, Dec 23, 2022
1 Year ago
lucianom questions
Tue, Feb 22, 22, 00:00, 2 Years ago
Wed, May 5, 21, 00:00, 3 Years ago
Sun, Jan 24, 21, 00:00, 3 Years ago
Sat, Aug 15, 20, 00:00, 4 Years ago
Mon, Jun 22, 20, 00:00, 4 Years ago
;