You can select all of those inputs with querySelectorAll
:
const inputs = document.querySelectorAll("input.leaflet-control-layers-selector");
That gives you a NodeList
, which you can loop through via forEach
(in modern browsers) or index into via length
and []
.
For instance, adding a button:
const list = document.querySelectorAll("input.leaflet-control-layers-selector");
for (const input of list) {
input.insertAdjacentHTML(
"afterend",
"<button type=button>Some Button</button>"
);
}
<div class="leaflet-control-layers-base">
<label>
<div><input type="radio" class="leaflet-control-layers-selector" name="leaflet-base-layers_157"><span> All Activities</span></div>
</label>
<label>
<div><input type="radio" class="leaflet-control-layers-selector" name="leaflet-base-layers_157"><span> <span id="icons1">Beaches</span></span></div>
</label>
<label>
<div><input type="radio" class="leaflet-control-layers-selector" name="leaflet-base-layers_157"><span> <span id="icons2">Wading pools</span></span></div>
</label>
<label>
<div><input type="radio" class="leaflet-control-layers-selector" name="leaflet-base-layers_157"><span> <span id="icons3">Swimming pools</span></span></div>
</label>
</div>
For slightly older browsers, you may need to use forEach
instead, and on even older ones, you may need to polyfill forEach
. This answer goes into that part of things in detail.
Or if you wanted the button
s at the end of the span
s:
const list = document.querySelectorAll("input.leaflet-control-layers-selector");
for (const input of list) {
input.nextElementSibling.insertAdjacentHTML(
"afterend",
"<button type=button>Some Button</button>"
);
}
<div class="leaflet-control-layers-base">
<label>
<div><input type="radio" class="leaflet-control-layers-selector" name="leaflet-base-layers_157"><span> All Activities</span></div>
</label>
<label>
<div><input type="radio" class="leaflet-control-layers-selector" name="leaflet-base-layers_157"><span> <span id="icons1">Beaches</span></span></div>
</label>
<label>
<div><input type="radio" class="leaflet-control-layers-selector" name="leaflet-base-layers_157"><span> <span id="icons2">Wading pools</span></span></div>
</label>
<label>
<div><input type="radio" class="leaflet-control-layers-selector" name="leaflet-base-layers_157"><span> <span id="icons3">Swimming pools</span></span></div>
</label>
</div>
Etc.