I'm using Bootstrap group radio button on which I would like to write event delegation.
<div class="btn-group" role="group" aria-label="Basic radio toggle button group">
<input type="radio" class="btn-check" name="btnradio" id="btnradio1" autocomplete="off" checked>
<label class="btn btn-outline-primary" for="btnradio1">Radio 1</label>
<input type="radio" class="btn-check" name="btnradio" id="btnradio2" autocomplete="off">
<label class="btn btn-outline-primary" for="btnradio2">Radio 2</label>
<input type="radio" class="btn-check" name="btnradio" id="btnradio3" autocomplete="off">
<label class="btn btn-outline-primary" for="btnradio3">Radio 3</label>
</div>
Since the labels are not nested in the button, the following is not returning the inputs.
event.target.closest('.btn-check');
Is there a proper way to write event delegation on this type of non-nested elements?
Here is how. Clicking the radio OR the label will show the ID. No need for
closesthere, but closest WILL return the element itself if it matches the selectorIn Bootstrap it also works
If you have buttons with child elements, you DO need
closestClicking the button text OR the icon will show the ID