I am attempting to access child elements within an unordered list, but I'm not getting the values I expected.
Here is my HTML code:
<ul id="weekdayList">
<li>
<label for="sunday">S</label>
<input id="sunday" type="checkbox" name="sunday">
</li>
<li>
<label for="monday">M</label>
<input id="monday" type="checkbox" name="monday">
</li>
<li>
<label for="tuesday">T</label>
<input id="tuesday" type="checkbox" name="tuesday">
</li>
<li>
<label for="wednesday">W</label>
<input id="wednesday" type="checkbox" name="wednesday">
</li>
<li>
<label for="thursday">T</label>
<input id="thursday" type="checkbox" name="thursday">
</li>
<li>
<label for="friday">F</label>
<input id="friday" type="checkbox" name="friday">
</li>
<li>
<label for="saturday">S</label>
<input id="saturday" type="checkbox" name="saturday">
</li>
</ul>
And the JS code:
$(document).ready(() => {
// Make labels active when corresponding checkbox is checked
$('#weekdayList label').click((event) => {
event.stopPropagation();
console.log($(this));
console.log($('#weekdayList li:nth-child(2) label'));
$(this).toggleClass('active');
});
});
CodePen here: https://codepen.io/mattmc318/pen/mdyEeJy
When I click on the 'M' label
, for example, I expect the output of console.log($(this));
to be the same as console.log($('#weekdayList li:nth-child(2) label'));
.
Instead of this:
Object { 0: label, length: 1, prevObject: Object(1) }
I get this:
Object [ Window ]
Is there something I'm doing wrong? Any help is appreciated.