How to check radio button on page load with jQuery

For example you have a set of radio buttons created dynamically with some web language like PHP and you expected one of them selected or checked on most of the times. If, none of them was checked somehow, you would want jQuery to get one of checked on page load. Here’s the quick solution to this condition.

You have three radio buttons for the selection of colors like this:


The following jQuery code does the job to select green color if none of them was selected, on page load or onload event.

$(document).ready(function() {
    var $colors = $('input:radio[name=color]');
    if($':checked') === false) {
        $colors.filter('[value=green]').attr('checked', true);

Leave a Reply