Skip to content
Advertisement

Select odd checkboxes

Select odd check boxes please help me with the code

for(let i=0;i<8;i++){
if(i%2==1){
document.querySelector(“ul.todo-list > li:nth-child(i) input.toggle”).click()
}
}

Advertisement

Answer

You can use the li:nth-child(odd) then set the checked attribute to true for those selected checkboxes.

This is an example:

let elements = document.querySelectorAll('ul.todo-list > li:nth-child(odd) input[type="checkbox"]');
for(var i=0;i<elements.length;i++){
    elements[i].checked = true;
}
<ul class="todo-list">
    <li>
        <input type="checkbox" />
    </li>
    <li>
        <input type="checkbox" />
    </li>
    <li>
        <input type="checkbox" />
    </li>
    <li>
        <input type="checkbox" />
    </li>
    <li>
        <input type="checkbox" />
    </li>
    <li>
        <input type="checkbox" />
    </li>
    <li>
        <input type="checkbox" />
    </li>
</ul>
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement