Skip to content Skip to sidebar Skip to footer

Calling The Same Function When Any Radio Button Or Checkbox Are Clicked

I have some radio button and checkboxs like

Solution 1:

Since you may have other input elements in the page, just use a class name to identify them. In my example, I used the class name calc so you can use it to define an on change event.

<scripttype="text/javascript">functioncalcPrice(){
    console.log('checked!!'); // note that you missed the quotes here
}
</script><inputclass="calc"type="radio"name="android"value="1">smart
<inputclass="calc"type="radio"name="android"value="2">tablet
<inputclass="calc"type="radio"name="android"value="3">both

<inputclass="calc"type=checkboxname="func"value="0">push
<inputclass="calc"type=checkboxname="func"value="0">distribute
<inputclass="calc"type=checkboxname="func"value="0">internationalization

For your JavaScript:

$("input.calc").change(function() {
    calcPrice();
});

Solution 2:

But I would like to call the same function when 'any' button are clicked.

You can use Multiple Selector (“selector1, selector2, selectorN”) and add :button selector in existing selector.

$("input[name='android'], input[name='func'], :button").change(function()

An equivalent selector to $( ":button" ) using valid CSS is $( "button, input[type='button']" ).

Solution 3:

Try this,

    $("input[name='android'], input[name='func']").change(function(){

    });

Solution 4:

you can use this

functioncalcPrice(){
    console.log("checked!!");
}

$("input[name='android'],input[name='func']").change(function()
{
    calcPrice()  ;                               
});

Post a Comment for "Calling The Same Function When Any Radio Button Or Checkbox Are Clicked"