Question

Is there any event, to append to the following listener, that will tell me if a radio button is already clicked?

var budgetType;
$(document).on('change', 'input[name="import_export"]', function() {
    console.log($(this))
    budgetType = $(this).val();
});

My problem is that, on page reload, if the radio box is already checked, the change listener won't work, and I didn't find any listener that will help me with that issue.

Any ideas?

Update

I'm aware that I could write an if/else syntax and see which is checked, but I was wondering if there is any other listener that I could append to change one, and avoid writing that extra syntax.

http://jsfiddle.net/S93XB/1/

Was it helpful?

Solution

$('input[type=radio]').is(':checked')

Try this code here: http://jsfiddle.net/afzaal_ahmad_zeeshan/rU2Fc/

or try this one

$('input[type=radio]').prop('checked', true);

Try this code here: http://jsfiddle.net/afzaal_ahmad_zeeshan/rU2Fc/1/

It will work, you can execute this onload to get the first value and use it in an if else block.

OTHER TIPS

Trigger the change event on load:

var budgetType;
$(document).on('change', 'input[name="import_export"]', function() {
    var isChecked = $(this).is(":checked");
    console.log($(this).val()+" "+isChecked);
    budgetType = $(this).val();
});
$('input[name="import_export"]:checked').trigger('change');

http://jsfiddle.net/eLGaB/

Try this :

$('input[type=radio]').is(':checked')

Or

$('input[type=radio]').prop('checked') === true;

Or

$('input[type=radio]').attr('checked') === 'checked';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top