Tobias Mühl
Tobias Mühl

Reputation: 2058

jQuery find() method not working on only one(!) class

jQuery's find() method is not working with everything except my class .szenenkasten. It doesn't make any sense at all...

Simplified source code:

HTML

<ul class="NavBar">
    <li></li>  
    <li></li> 
    <li></li> 
    <li></li> 
    <li></li>         
</ul>

<span class="szenenkasten"></span>
<span class="szenenkasten"></span>
<span class="szenenkasten"></span>
<span class="szenenkasten"></span>
<span class="szenenkasten"></span>

jQuery

alert($('.NavBar').find('li').length);
 //returns 5
alert($('.szenenkasten').find('span').length);
 //returns 0
alert($('span').find('.szenenkasten').length);
 //returns 0
alert($('span.szenenkasten').length);
//returns 5

Upvotes: 1

Views: 1082

Answers (1)

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382150

find looks among descendants of your jquery elements. Here your span aren't descendant but the same elements.

Don't use find but filter here :

alert($('.szenenkasten').filter('span').length);

or simply

alert($('span.szenenkasten'));

Upvotes: 4

Related Questions