I'mNoob I'mNoob - 4 months ago 22
jQuery Question

Check if there is a label inside li

I tried to check if there is a label inside the

li
with find children but I'm newbie and can't do it right I know it's easy but I'm new in Jquery :

HTML :

<li class="dropdown open">
<div class="dropdown-toggle">
Something
</div>
<ul>
<li>
<input id="test" type="checkbox" value="Test" class="checkbox">
<label for="test">Test</label>
</li>
</ul>
</li>

Answer

You could check the length :

if( $('li').find('label').length )
    //exist
else
    //does not exist

Hope this helps.

if( $('li').find('label').length )
  console.log('Label exist');
else
  console.log('Label does not exist');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="dropdown open">
    <div class="dropdown-toggle">
        Something
    </div>
    <ul>
        <li>
            <input id="test" type="checkbox" value="Test" class="checkbox">
            <label for="test">Test</label>
        </li>
    </ul>
</li>

Comments