arthur arthur - 3 months ago 9
Javascript Question

jQuery select all inputs under ul li

How can I select all input checkboxes in li under a specific ul?

I have:

<ul id="something">
<li>
<input type="checkbox">
</li>
<li>
<input type="checkbox">
</li>
...
</ul>


Thanks

Answer

You can use $('ul li input[type=checkbox]')

Here is a working example, you can see that the console.log will show only input's a, b, d (because c's type is not checkbox).

checkboxes = $('ul li input[type=checkbox]')

checkboxes.each(function(e) {
  console.log($(this).attr('name'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="something">
    <li>
        <input name="a" type="checkbox">
    </li>
    <li>
        <input name="b" type="checkbox">
    </li>
    <li>
        <input name="c" type="text">
    </li>
    <li>
        <input name="d" type="checkbox">
    </li>
</ul>

Comments