[jQuery] [jquery] if element exist
Someone will correct me if I'm wrong, but if you simply want to execute your
own logic based on the existence of an object, then the following snippet
if ($("#name).size() > 0)
{ ... your logic without any jQuery methods ... }
is inefficient since $() does quite a bit of work when it wraps itself
around an object.
If you are going to apply some jQuery methods to the item, then this is
better:
var n = $("#name");
if (n.size() > 0)
{
... Your logic ...
n.method();
}
By saving $("#name") in a variable, you don't needlessly execute the $()
logic repeatedly.