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.
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.
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
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly