Call JavaScript function when RadioButton is checked or unchecked

Submitted by:Jhon Smith

Date added:19 September, 2017

Category:ASP

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");
dvPassport.style.display = chkYes.checked ? "block" : "none";
}
</script>
<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" />
</div>
 
 

Comments