Reputation: 28513
I have a function in Jquery Mobile called findclosestLink, that checks what is clicked when the user clicks on screen.
Looks like this:
var link = find(findClosestLink(event.target);
findClosestLink: function (ele) {
var self = this;
while (ele){
if (ele.nodeName.toLowerCase() == "a"){
break;
}
ele = ele.parentNode;
}
console.log(ele);
return ele;
}
If I click somewhere in the screen ele consoles as "null" and my variable becomes "[]".
Question: how can I check for this empty something "[]"?
All of these don't work:
if ( link == 'null' ) { console.log("null"); }
if ( link == '' ) { console.log("empty");}
if ( link == 'undefined' ) { console.log("undefined"); }
if ( link == []) { console.log("empty object"); }
if ( link == $() ) { console.log("empty dings"); }
Thanks for help!
Upvotes: 1
Views: 1030
Reputation: 5509
To explain my comment a bit more:
if ( typeof link == 'null' ) { console.log("null"); }
if ( link == '' ) { console.log("empty");}
if ( typeof link == 'undefined' ) { console.log("undefined"); }
if ( typeof link == 'object') { console.log("empty object"); }
or
switch(typeof link) {
case 'null':
console.log('is null');
break;
[....]
}
Upvotes: 0
Reputation: 342695
Do you mean check for empty array? Use the length
property:
console.log([].length); // 0
so:
// 0 is 'falsy', so...
if(!ele.length) {
// empty
}
Upvotes: 1