Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the label of the selected radio button using javascript

I have the following HTML

<div class="form-radios" id="edit-submitted-lunchset-lunch"><div class="form-item form-type-radio form-item-submitted-lunchset-lunch">
<input type="radio" class="form-radio" value="1" name="submitted[lunchset][lunch]" id="edit-submitted-lunchset-lunch-1">  <label for="edit-submitted-lunchset-lunch-1" class="option">12:00 </label>

</div>
<div class="form-item form-type-radio form-item-submitted-lunchset-lunch">
<input type="radio" class="form-radio" value="2" name="submitted[lunchset][lunch]" id="edit-submitted-lunchset-lunch-2">  <label for="edit-submitted-lunchset-lunch-2" class="option">12:30 </label>

</div>
<div class="form-item form-type-radio form-item-submitted-lunchset-lunch">
<input type="radio" class="form-radio" value="3" name="submitted[lunchset][lunch]" id="edit-submitted-lunchset-lunch-3">  <label for="edit-submitted-lunchset-lunch-3" class="option">13:00 </label>

</div>
<div class="form-item form-type-radio form-item-submitted-lunchset-lunch">
<input type="radio" class="form-radio" value="4" name="submitted[lunchset][lunch]" id="edit-submitted-lunchset-lunch-4">  <label for="edit-submitted-lunchset-lunch-4" class="option">13:30 </label>

</div>
<div class="form-item form-type-radio form-item-submitted-lunchset-lunch">
<input type="radio" class="form-radio" value="5" name="submitted[lunchset][lunch]" id="edit-submitted-lunchset-lunch-5">  <label for="edit-submitted-lunchset-lunch-5" class="option">14:00 </label>

</div>
<div class="form-item form-type-radio form-item-submitted-lunchset-lunch">
<input type="radio" class="form-radio" checked="checked" value="6" name="submitted[lunchset][lunch]" id="edit-submitted-lunchset-lunch-6">  <label for="edit-submitted-lunchset-lunch-6" class="option">14:30 </label>

</div>
</div>

I am trying to retrieve the label associated with the radio button instead of the value using the following javascript but the variable "chosentime" is still "unassigned". I checked my code by throwing in a few alerts the alert(radios[i].innerhtml); throws unassigned

var radios = document.getElementsByName('submitted[lunchset][lunch]');
for (var i = 0, length = radios.length; i < length; i++) {
    if (radios[i].checked) {
        alert(i);
        alert(radios[i].value);
        alert(radios[i].innerhtml);
        chosentime = radios[i].innerhtml;
    }
}
window.alert(chosentime);
}

Would appreciate any help. Thanks in advance.

like image 290
sridhar pandurangiah Avatar asked Feb 17 '23 15:02

sridhar pandurangiah


1 Answers

You have to access the corresponding label separately, since it resides in a separate tag. Because the labels' for attribute is equal to its option's id, you can get to the label easily:

for(var i=0; i<radios.length; i++) {
    var selector = 'label[for=' + radios[i].id + ']';
    var label = document.querySelector(selector);
    var text = label.innerHTML;
    // do stuff
}

Also check out this fiddle

like image 64
MCL Avatar answered Mar 05 '23 16:03

MCL