Call JavaScript function when RadioButton is checked or unchecked

Submitted by:Jhon Smith

Date added:19 September, 2017


Note: The ASP.Net RadioButtons must be set with exact same GroupName property in order to make them mutually exclusive.

Tags: javascript function , radiobutton

Code Snippet:

<script type="text/javascript">
function ShowHideDiv() {
var chkYes = document.getElementById("<%=chkYes.ClientID %>");
var dvPassport = document.getElementById("dvPassport"); = chkYes.checked ? "block" : "none";
<span>Do you have Passport?</span>
<asp:RadioButton ID="chkYes" GroupName="Passport" Text="Yes" runat="server" onclick="ShowHideDiv()" />
<asp:RadioButton ID="chkNo" GroupName="Passport" Text="No" runat="server" onclick="ShowHideDiv()" />
<hr />
<div id="dvPassport" style="display: none">
Passport Number:
<input type="text" id="txtPassportNumber" />