Reputation: 19358
I don't know why this doesn't work, I'm just trying to check if .searchBar
doesn't exist.
var $school = "Washington";
if(!$('.searchBar')){
$('#schoolname').text($school);
}
Upvotes: 14
Views: 18833
Reputation: 79830
Use .length
to find if it exist if(!$('.searchBar').length){
jQuery $()
function always return a jQuery object even if doesn't find any element. So you need to use .length
property of the jQuery object to find if the element actually exist.
Upvotes: 12
Reputation: 23786
if($('.searchBar').length === 0){
Remember, jquery always returns a wrapped set of matching elements. The list may be zero though.
Upvotes: 26