I have a set of radio buttons, with the first one being statically set at the time of rendering the html. The remaining are dynamically created using jQuery. The code for creating the radio buttons is given below
for (var i = 1; i < length; i++){
$('.radio').append('<br>');
$('.radio').append('<label><input type = "radio" id = '+options[i]+' value = '+i+' name = "options" class = "radio-options"/><p>'+options[i]+'</p></label>');
}
//creating the last radio button here
$('.radio').append('<br>');
$('.radio').append('<label><input type = "radio" id = "Custom" value = '+length+' name = "options" class = "radio-options"/><p>Custom</p></label>')
I have a submit button, which when I click, I want to check which of the radio buttons has a checked property attribute. Here's my attempt at doing that.
$('.submit-butt').on('click', function(){
I print the checked attribute status of all the radio buttons immediately after the submit button click handler is invoked. They all come out to be false, regardless of whether I have a radio button selected or not.
for(var i = 0; i < options_length; i++){
console.log($('input:radio[name=options]').prop('checked'));
}
if($('input:radio[name=options]').prop('checked', true)){
console.log('Found my value');
var _this = $('input:radio[name=options]:checked');
But, for some reason this console log statement prints out Custom as the id, which is the id of the last radio button, even though i haven't selected it.
console.log(_this.attr('id'));
Here is where I print the checked attribute of all the radio buttons a second time. It comes out to be false here as well, including the Custom id radio button, which printed out in the previous line.
for(var i = 0; i < options_length; i++){
console.log($('input:radio[name=options]').prop('checked'));
}
}
});
Regardless of whether I choose a radio button or not, and regardless of which radio button that is, when I hit the submit button, the Custom radio button is immediately selected, even though the checked property of that radio button still prints to false. How do I solve this issue?