dezman
dezman

Reputation: 19358

How can I check if a class doesn't exist?

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

Answers (2)

Selvakumar Arumugam
Selvakumar Arumugam

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

aquinas
aquinas

Reputation: 23786

if($('.searchBar').length === 0){

Remember, jquery always returns a wrapped set of matching elements. The list may be zero though.

Upvotes: 26

Related Questions