I have an element called link which I need to check.
Currently I’m doing this:
// routine
if ( link.length == 1 && !link.is( ".toggle_popover" ) ) {
self.panelTrans();
}
I’m curious regarding my check for link
I have tried the following:
if (link) ...
if (!link)...
if (link.length == 1) ...
if (link.length != 0) ...
The problem is link can also be an empty object = [], in which case I don’t want my routine to fire.
Which way is best to do this? Right now I’m sticking with link.length == 1
Thanks for help!
UPDATE
link is the following:
var link = $( self.findClosestLink(event.target) )
findClosestLink: function (ele) {
var self = this;
while (ele){
if (ele.nodeName.toLowerCase() == "a"){
break;
}
ele = ele.parentNode;
}
return ele;
}
It’s taken from the jQuery Mobile script and checks when the user clicks on screen if there is a parent link element. I’m using this to fire my own routine along JQM and want to make sure I’m not triggering any wasted function call. That’s why I’m checking for link. On Firebug I sometimes ended up with the function being triggered on an empty object [], which I do not want it to fire on.
Therefore my question on how to check for link
i think that the best way is (i suppose you are speaking of a jQuery object)
with an added
=so there is no type coercion. If you use anidselector this is perfect as the selector returns only one element (this of course implies that your ids are unique on your page).if you must check if at least one element exist in a collection (You use a
classselector) of course use