webmonkey237 webmonkey237 - 4 months ago 10
jQuery Question

jQuery: Radio buttons not getting checked

I have a project where I am using radio button selection to change a class, easy stuff.

FIDDLE

This is my first time working with radio buttons with a script so what I can't work out is why the radio buttons aren't appearing checked/selected, I even added some jQuery to force them to be "checked" still with no luck.

$('input.eaBtn[type="radio"]').click(function (e) {
e.preventDefault();
console.log($(this).data('class'))
$('.eaStaff').removeClass('transBg darkBg lightBg customBg');
$('.eaStaff').addClass($(this).data('class'));
$(this).attr('checked', true);
});

Answer

Remove the e.preventDefault();

This is the reason it is preventing the field to choose this option.

And after that, you dont need this line anymore $(this).attr('checked', true);

jsfiddle

CODE:

$('input.eaBtn[type="radio"]').click(function (e) {
    //e.preventDefault();
    console.log($(this).data('class'))
    $('.eaStaff').removeClass('transBg darkBg lightBg customBg');
    $('.eaStaff').addClass($(this).data('class'));
    //$(this).attr('checked', true);
});