Title: jQuery - find a class exist and return true/false
Tags: javascript;jquery;find
Question: I want to check if class exists inside a child of ```li``` and if class exists return ```true``` or ```defined```.

HTML

```<li class="show-more"&gt;
<a&gt; if found selected then add to this</a&gt;
<ul&gt;
<li&gt;<a&gt; sub-menu</a&gt;</li&gt;
<li&gt;<a&gt; sub-menu</a&gt;</li&gt;
<li&gt;<a class="selected"&gt; sub-menu</a&gt;</li&gt;
<li&gt;<a&gt; sub-menu</a&gt;</li&gt;
</ul&gt;
</li&gt;
```

here is my code, but wont return true or false, it return the url of anchor

```var active_sub_menu = $('li.show-more ul li').find('selected');

alert(active_sub_menu);
if(typeof active_sub_menu == 'defined'){
    $('li.show-more &gt; a').addClass('selected');
}
```

please dont suggest to use CSS, I just need javascript

Comment: `find('selected')` and as you can see `'selected'` !== `'.selected'`

Here is the accepted answer: Why not simply go for ```.hasClass()``` or ```length```:

```if ( $("selector").length ) {   /*EXISTS (greater than 0) */  }
```

or 

```if ( $("selector")[0] ) {   /*EXISTS (not undefined) */  }
```

or

```if ( $("selector").hasClass("someClass") ) {   /*EXISTS (has class) */  }
```



```$('.show-more').each(function () {

  if ( $(this).find("li a.seleted").length ) {
    console.log('FOUND');
    $(this).children("a").addClass('selected'); // Make main anchor  also selected 
  } else {
    console.log('NOT FOUND');
  }

});```
```.selected {
  background: red;
}```
```<ul&gt;
  <li class="show-more"&gt;
    <a&gt; if found .selected then add also to this anchor</a&gt;
    <ul&gt;
      <li&gt;<a&gt; sub-menu</a&gt;</li&gt;
      <li&gt;<a&gt; sub-menu</a&gt;</li&gt;
      <li&gt;<a class="selected"&gt; sub-menu</a&gt;</li&gt;
      <li&gt;<a&gt; sub-menu</a&gt;</li&gt;
    </ul&gt;
  </li&gt;
</ul&gt;


<script src="//code.jquery.com/jquery-3.1.0.js"&gt;</script&gt;```




Comment for this answer: please check updated question, there is an anchor tag just after list tag and that is the place where I want to add selected class if found in its child `ul li a`

Here is another answer: use ```length``` to test if selector returns elements

```if( $('li.show-more ul li').find('selected').length){
   $('li.show-more &gt; a').addClass('selected');
}
```

Comment for this answer: it is not working, I tried `alert($('li.show-more ul li').find('qa-nav-main-selected').length);` and it return `0` but there is `selected` class in a child

Here is another answer: ```if($('li.show-more ul li').hasClass('defined')) {
    $('li.show-more &gt; a').addClass('selected');
}
```

I assume that's what you intended with ```typeof```? 

Another option:

```var active_sub_menu = $('li.show-more ul li').find('.selected');
if(active_sub_menu.length) {
    $('li.show-more &gt; a').addClass('selected');
}
```

Comment for this answer: `active_sub_menu.length` also returning `0`??

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:02/15/13', 'FavoriteCount': 4.0, 'PostTypeId': 1, 'Tags': '<javascript><jquery><find>', 'Title': 'jQuery - find a class exist and return true/false', 'ViewCount': 93164, 'id': 14887055, 'user_AboutMe': '', 'user_AccountId': 2250720.0, 'user_DisplayName': 'user007', 'user_Reputation': 3085.0, 'user_Views': 448.0, 'user_id': 1983017.0}}