How to call jQuery function on radio button change event?


I would like to call jQuery function on <input type='radio' /> button change. How can I do it?

I have to show, hidden <div> on radio button value='yes', so if I have two <div>, with id div1 & div2, I would like to show div2 when radio value is "yes", here is my example HTML

<div>
               <input type="radio" id="128" name="53" value="Yes" class="RadioBtn">Yes              
               <input type="radio" id="129" name="53" value="No" class="RadioBtn">No                
</div>


<div id="1">
</div>

<div id="2">
data 2 
</div>

Would need jQuery code to show div on radio button change, using a function.


Asked by:- jaiprakash
1
: 4379 At:- 3/16/2018 2:37:46 PM
jQuery radio button change







2 Answers
profileImage Answered by:- jaya

You can use the on 'change' event of the radio button and show/hide div depending on your need.

Example:

$(document).ready(function() {
    $('input[type=radio][name=53]').change(function() {
        if (this.value == 'Yes') {
            $("#2").show();
        }
        else if (this.value == 'No') {
             $("#2").hide();
        }
    });
});

Here is the working fiddle link https://jsfiddle.net/r2fdkjnt/

Note: I am hiding <div> 2 on load.

2
At:- 3/17/2018 1:29:16 PM
thanks for your easy solution and fiddle link 0
By : jaiprakash - at :- 3/23/2018 7:14:33 AM


profileImage Answered by:- vikas_jk

You can also use Inline onchange Javascript method

               <input type="radio" id="128" name="53" value="Yes" class="RadioBtn"  onchange="changeRadio('Yes')">Yes              
               <input type="radio" id="129" name="53" value="No" class="RadioBtn" onchange="changeRadio('No')">No        

Javascript

function changeRadio(val){
    console.log(val);
 }

check fiddle: https://jsfiddle.net/jqo1k34c/

Thanks

0
At:- 12/15/2022 3:22:48 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use