Home > Enterprise >  Jquery click function only seems to be working once
Jquery click function only seems to be working once

Time:09-24

I'm trying to pre-select a radio button based on a button click further up the page. The below code does work but it only seems to correctly select the radio option once.

jQuery(document).ready(function(){
  jQuery(".select-one").on('click', function(){
    jQuery("input[type='radio'][id='choice_25_25_0']").attr("checked", true);
});
   jQuery(".select-two").on('click', function(){
    jQuery("input[type='radio'][id='choice_25_25_1']").attr("checked", true);
});
   jQuery(".select-three").on('click', function(){
    jQuery("input[type='radio'][id='choice_25_25_2']").attr("checked", true);
});
});

Is there anything I can add to this code to enable it to run each time the button is clicked? Any help much appreciated.

JFiddle Example

CodePudding user response:

change to .prop() instead of .attr()

jQuery(document).ready(function(){
  jQuery(".select-one").on('click', function(){
    jQuery("input[type='radio'][id='choice_25_25_0']").prop("checked", true);
});
   jQuery(".select-two").on('click', function(){
    jQuery("input[type='radio'][id='choice_25_25_1']").prop("checked", true);
});
   jQuery(".select-three").on('click', function(){
    jQuery("input[type='radio'][id='choice_25_25_2']").prop("checked", true);
});
});
  • Related