I have a simple JS code which needs to get the IDs from each radio button input, and then create labels with the same IDs for each radio button. The code works, but I am getting an error message as written in the title. How to get rid of it?
And I saw there's already a similar question but it didn't solve my problem at all.
function addLabels() {
var radioButton = document.getElementsByTagName("input");
for (var i = 0; i <= radioButton.length; i++) {
var radioButtonId = radioButton[i].id;
var label = "<label for='" + radioButtonId + "'></label>";
$(label).insertAfter(radioButton[i]);
}
}
addLabels();
<div class="slider-nav">
<input type="radio" name="slider-button" id="radio-button0">
<input type="radio" name="slider-button" id="radio-button1">
<input type="radio" name="slider-button" id="radio-button2">
</div>