Click here to Skip to main content
Licence CPOL
First Posted 1 Jul 2011
Views 9,454
Bookmarked 7 times

Show/Hide the Div with RadioButtonList Using JavaScript

By | 1 Jul 2011 | Article
Making the div show/hide based on the selected radiobutton from the radiobuttonlist using JavaScript.

Introduction

Here, I show a simple way of getting the selected/checked radiobuttonlist and show and hide a div using JavaScript.

The below page is the default.aspx which has the JavaScript method by name GetRDBValue and the parameters are sent by adding attribute onclick in the code behind (default.aspx.cs) i.e., page_load of the same.

Here, if the radiobutton yes is selected, I show the div and if no is selected, I hide the div. The rest of the code is self explanatory.

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true"  
CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>    
      <script type="text/javascript" language="javascript">
      function GetRDBValue(showw)
        {
        debugger;
        var showdiv = document.getElementById(showw);
            var radio = document.getElementsByName('rdbGender');
             if(radio.length > 0)
            {
	            for(var j=0 ; j < radio[0].cells.length ; j++)
	            {
		            for(var k=0 ; k < radio[0].cells[j].children.length ; k++)
		            {
			            if(radio[0].cells[j].children[k].checked == 
			            true && radio[0].cells[j].innerText == "Yes")
			            {
			              showdiv.style.display ="block" ;

			            }
			            else if(radio[0].cells[j].children[k].checked == 
			            true && radio[0].cells[j].innerText == "No")
			            {
			            showdiv.style.display = "none";
			            }
		            }
	            }
            }
    }
        </script>

</head>
<body>
    <form id="form1" runat="server">
    
    <div id="checkdiv" runat="server" 
    style="display:none">Lokesh perumbali</div>
    <asp:Panel ID="pnl1" runat="server" 
    style="display:none">Lokesh test</asp:Panel>
    <div>
    <asp:RadioButtonList ID="rdbGender" runat="server">
            <asp:ListItem Text="Yes" Value="1"></asp:ListItem>
            <asp:ListItem Text="No" Value="2"></asp:ListItem>            
</asp:RadioButtonList>

    </div>
    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        rdbGender.Attributes.Add("onClick", 
		string.Format("GetRDBValue('{0}');", checkdiv.ClientID));
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

lperumb

Software Developer
Wipro Technologies
India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberOmar Gamil10:46 1 Jul '11  
SuggestionNot really an article PinPopularmentorDaveAuld6:50 1 Jul '11  
GeneralRe: Not really an article PinmvpAspDotNetDev11:11 1 Jul '11  
GeneralRe: Not really an article PinmvpRichard MacCutchan21:51 1 Jul '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 1 Jul 2011
Article Copyright 2011 by lperumb
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid