I have multiple sections created dynamically and need to add an event listener to the following check boxes. Note: I don't want to select all the checkboxes, just those wrapped in the specified DIV tags (which are created dynamically in code I cannot modify).
EDIT: (as this question was INCORRECTLY marked as a duplicate) I do not want to add events to elements as more elements are dynamically added to the page. I am trying to add an event to multiple sections of the DOM when the page first loads. (Found the link to dispute the duplicate and it wants me to change something)
EDIT: (For the 2nd INCORRECTLY marked as a duplicate question) That 2nd duplicate link still does not apply. I already know how to attach an event to the HTML as .addEventListener will work, I am looking to FIND all the relevant checkbox/elements scattered through the DOM and then loop through them to add the event. Please read the entire question before marking it down.
<div class="keep-open btn-group" title="Add Listener">
<ul class="dropdown-menu" role="menu"><li><label><input type="checkbox" data-field="1" value="0" checked="checked"> Category</label></li>
<li><label><input type="checkbox" data-field="2" value="1" checked="checked"> Item 1</label></li>
<li><label><input type="checkbox" data-field="3" value="2" checked="checked"> Item 2</label></li>
<li><label><input type="checkbox" data-field="4" value="3" checked="checked"> Item 3</label></li>
</ul>
</div>
<label>
<input type="checkbox" checked="checked"> Not this checkbox
<label>
<div class="keep-open btn-group" title="Add Listener to 1 of many">
<ul class="dropdown-menu" role="menu"><li><label><input type="checkbox" data-field="1" value="0" checked="checked"> Category</label></li>
<li><label><input type="checkbox" data-field="2" value="1" checked="checked"> Item 4</label></li>
<li><label><input type="checkbox" data-field="3" value="2" checked="checked"> Item 5</label></li>
<li><label><input type="checkbox" data-field="4" value="3" checked="checked"> Item 6</label></li>
</ul>
</div>
The following gives me over a dozen sections (in the real code) to add event listeners to:
var theBoxSections = document.getElementsByClassName("dropdown-menu");
This bit of code doesn't work, but will give an idea of what I am looking for:
$('#dropdown-menu input[type="checkbox"]').addEventListener("change", function () {
alert("Checkbox clicked")
});
I have come close with .addEventListener, yet struggling to get all the relevant checkboxes from multiple sections of the DOM.