Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused about when saving a variable using the $() function

Tags:

jquery

In jQuery, if I do this:

var a = $("#someid");

Now when I need to further reference using jQuery, what should I be doing?

$(a).attr("id");

or

a.attr("id");

I'm testing things out and I'm getting confused, just want the official word so I can rule this out.

like image 770
Blankman Avatar asked Dec 16 '22 18:12

Blankman


1 Answers

This one:

a.attr("id");

since a is already a jQuery object.

Although it is a convention used by many to prefix variables that reference a jQuery object with $.

So:

var $a = $("#someid");
$a.attr("id");

This is only a common convention, and not a requirement. I think it adds clarity, but that may be just because I'm pretty conditioned to look for the $ by now.

like image 85
user113716 Avatar answered Dec 19 '22 08:12

user113716