Is there any specific reason behind using $ with variable in jQuery
Solution 1:
No there is no real difference.
It's just a convention that helps you remember that a
isn't the DOM element but it's a jQuery object.
var a = document.getElementById('a');
a.innerHTML //fine
var $a = $('#a');
$a.html() // fine
Ohhh, and by the way, neither a
or $a
are good variable names ... you should use meaningful variable names not abc characters.
Read the jQuery info tag on this very same site:
Variable Naming Conventions
jQuery wrapped variables are usually named starting with '$' to distinguish them from standard JavaScript objects.
var $this = $(this);
Solution 2:
It's only for showing that it's a Jquery variable.
Declaring $a
you're showing that your variable is for JQuery objects, it's just a notation. So the most readable thing will be to declare Jquery variable with $ notation
var $obj=$("#obj");
And DOM element without $ notation
var obj = document.getElementById("obj");
Solution 3:
There's no difference. It's just a coding convention to help identify that the variable represents a jquery wrapped object.