Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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); 
  }
like image 557
dezman Avatar asked Feb 07 '13 19:02

dezman


2 Answers

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

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

like image 137
aquinas Avatar answered Oct 25 '22 13:10

aquinas


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.

like image 44
Selvakumar Arumugam Avatar answered Oct 25 '22 12:10

Selvakumar Arumugam