2

Here is my code

jQuery('#choice_1_10_3_1').attr('checked','checked');

I want to check this radio button when the page load. I could not found error in console but my code is not working.

It is essential for me to do. Any help will be appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

2 Answers2

2

The code will be run after the page is fully loaded.

Try this :

$(window).load(function() {
    $('#choice_1_10_3_1').attr('checked', true);
});
  • While this may work, you should really be using `prop()` for this as of jQuery 1.6: http://stackoverflow.com/questions/5874652/prop-vs-attr – Donnie D'Amato Aug 23 '16 at 11:04
0

It is possibly executing before the DOM is ready to be queried, try this:

jQuery(function() {
    jQuery('#choice_1_10_3_1').attr('checked','checked');
});

It might also be that you need to use .prop('checked', 'checked') instead of attr. I can never remember!

jedifans
  • 2,287
  • 1
  • 13
  • 9