I have HTML in this structure:
<ul class="list">
<li>name</li>
<li>name</li>
<li>name</li>
<li>name</li>
</ul>
I want check if the count of li
elements is even. How can I use this in an if
statement?
I have HTML in this structure:
<ul class="list">
<li>name</li>
<li>name</li>
<li>name</li>
<li>name</li>
</ul>
I want check if the count of li
elements is even. How can I use this in an if
statement?
if ($('ul>li').length % 2 == 0)
{
// do if even
}
else
{
// do if not
}
Also, if you need to apply to the even items something you can use:
$('ul>li:even').addClass('even')
Or odd:
$('ul>li:odd').addClass('even')