Can't get loop to spit out a string of the values in checked checkboxes.
Basic Javascript.
I've tried to follow various other stackoverflow posts to no avail. This and This seemed to be what was closest to what I'm trying to make work.
The HTML is just a row of
<div class="help-days">MON<br><input type="checkbox" id="d0-field" value="Monday" aria-describedby="avail-help"></div>
I've tried
var element = document.getElementsByClassName('help-days');
for (var i = 0; i <= 6; i++) {
if (element[i].checked) {
var day = $('#d' + i + '-field').val();
days = days + ' ' + day;
}
}
and
for (var i = 0; i <= 6; i++) {
var element = document.getElementById('#d' + i + '-field')
if (element[i].checked) {
var day = $('#d' + i + '-field').val();
days = days + ' ' + day;
}
}
Below example outputs 'Monday Tuesday Wednesday Thursday Friday Saturday Sunday' which leads me to believe there's something about using HTMLCollection and for loops & checking checkboxes that I'm not quite grasping?
for (var i = 0; i <= 6; i++) {
var day = $('#d' + i + '-field').val();
if (day) {
days = days + ' ' + day;
}
}
I'm trying to create a string that appends a checkbox 'value' to the string, if the checkbox is checked.
Any help appreciated!
Aucun commentaire:
Enregistrer un commentaire