Lord Vermillion
Lord Vermillion

Reputation: 5424

get text value from selected radiobutton with jquery

I'm trying to get the value of a selected radio button with jQuery. I generate my html-elements with jquery from a json-template, this is my code for generating radio buttons:

function AddRadio(labelName, buttons, i, paneIndex, dataID) {
        $('#Ctabs1-pane' + paneIndex + '').append('<div class="form-group"><label class="col-lg-2 control-label">' + labelName + '</label><div class="col-lg-10"  id="' + dataID + '"></div></div>');
        $.each(buttons, function (j, value) {
            $('#' + dataID).append('<div class="radio"><label><input type="radio" name="optionsRadios" value="' + value + '">' + value + '</label></div>');
        });
    }

i try to get the selected radiobutton with this code:

return $("input[type='radio'].radioBtnClass:checked").val();

But this gives me undefined

What am i doing wrong?

Upvotes: 1

Views: 3140

Answers (1)

Felix
Felix

Reputation: 38102

Your selector match input element with class radioBtnClass but you're not added it when append. Either add that class into your input:

$('#' + dataID).append('<div class="radio"><label><input type="radio" class="radioBtnClass" name="optionsRadios" value="' + value + '">' + value + '</label></div>');

or target it by name instead:

$("input[type='radio'][name='optionsRadios']:checked").val();

Upvotes: 2

Related Questions