Let's assume we have the following radio buttons
<input type="radio" value="0" name="radiogroup">
<input type="radio" value="1" name="radiogroup">
<input type="radio" value="2" name="radiogroup">
and on each click on a separate radio it should make something. Here is the code:
$(document).ready(function() {
$("input[name=radiogroup]:radio").click(function() { // attack a click event on all radio buttons with name 'radiogroup'
if($(this).val() == '0') {//check which radio button is clicked
//do something
} else if($(this).val() == '1') {
//do something else
} else if($(this).val() == '2') {
//do something else again
}
});
});
But how to trigger a click on exact radio button when the page is loaded? Here is how:
$("input:radio[name=radiogroup][value=1]").trigger('click');
// here we are triggering click event on the radio button with name 'radiogroup' ans value '1'