I'm trying to figure out how to access elements using jquery's .each function.
I have an unordered list of checkboxes and labels and I want to be able to access the data so I can do various things like check all the boxes or get the values in the selected labels.
I'm assuming I can rig it so that I can use the ID property to do this but I thought it would be more versatile if I actually knew how the .each statement works. :)
Primarily I'm interested in putting the selected label values into an array but any generic info would be appreciated.
$(document).on("click", "#test-button", function() {
$("#test-ul li").each(function(i, item) {
alert(item.children[0].value); //works
alert(item.children(".label")[0].value); //doesnt work
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id='test-ul'>
<li>
<input type='checkbox' id='1' class='checkbox' value='111' />
<label class='label' for='1' value='aaa'>aaa</label>
</li>
<li>
<input type='checkbox' id='2' class='checkbox' value='222' />
<label class='label' for='2' value='bbb'>bbb</label>
</li>
<li>
<input type='checkbox' id='3' class='checkbox' value='333' />
<label class='label' for='3' value='ccc'>ccc</label>
</li>
</ul>
<button id="test-button">Test</button>