Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if list item has more than 1 direct span child in jquery

Tags:

jquery

How do I check if a list item has more than 1 direct span child element in jquery? I have something now like:

if ( $('li:has(span)') ) {
   alert ('has span');
}

That will alert already when there's one span child, and also if there are nested li's who themselves have one or more span child elements.

like image 325
C.A. Vuyk Avatar asked Nov 26 '25 06:11

C.A. Vuyk


1 Answers

This will alert if there is 2 or more child span elements.

$('li').each(function(){
    if($('> span',this).length >= 2)
    {
       alert('has span');
    }
});

or using A. Wolff filter answer.

$('li').filter(function(){return $('> span',this).length > 1;});

http://jsfiddle.net/6tzbJ/

like image 200
Reactgular Avatar answered Nov 29 '25 00:11

Reactgular



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!