SIGN UP MEMBER LOGIN:    
ARTICLE

Show and hide div using JavaScript Function

Posted by Purushottam Rathore Articles | ASP.NET Programming September 02, 2010
In this article I am describing how to show and hide the content of div or table using JavaScript function.
Reader Level:

In this article I am describing  how to show and hide the content of div or table using JavaScript function.

<%@ 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>Example: Show hide div using JavaScript</title> 
<%--Javascript function--%>
<script type="text/javascript">
function DivExpandFun(divid, FixedImg, upImg, downImg) 
{
    if (typeof ccollect != "undefined"
    { 
        if (document.getElementById(divid).style.display != "none"
        { 
            document.getElementById(divid).style.display =
"none" 
            document.getElementById(FixedImg).src = downImg; 
            document.getElementById(FixedImg).title =
"Open div" 
        } 
        else 
        { 
            document.getElementById(divid).style.display =
"block" 
            document.getElementById(FixedImg).src = upImg; 
            document.getElementById(FixedImg).title =
"Close div" 
        } 
    }

function GetStyle(classname) 

    ccollect =
new Array()
}

function do_onload() 

    GetStyle(
"hideContent")//give the name of style class
}

if
(document.getElementById)
window.onload = do_onload

</script> 

<style type="text/css">
.hideContent

    display: block;
}
</style>

</
head>
<
body> 
<form id="form1" runat="server"> 
<div> 
    <table style="width: 250px; border: solid 1px gray; background-color: Silver;"> 
        <tr> 
            <td> 
                <span>My Details</span> 
            </td> 
            <td align="right">
<a onclick="DivExpandFun('Mydiv','Image1','<%=ResolveUrl("~/Images/UpArrow.gif")%>','<%=ResolveUrl("~/Images/DownArrow.gif")%>');">
<img id="Image1" src='<%=ResolveUrl("~/Images/UpArrow.gif")%>' title="Close div" /></a> 
            </td> 
        </tr> 
    </table> 

    <
div id="Mydiv" class="hideContent"> 
        <table style="padding: 5px; width: 250px; border: solid 1px gray"> 
            <tr> 
                <td> 
                    <b>Name:</b> Purushottam Rathore<br /> 
                    <b>Address:</b><br /> 
                    <b>City:</b> Noida<br /> 
                    <b>Pin Code:</b> 201301<br /> 
                    <b>Country:</b> India 
                </td> 
            </tr> 
        </table> 
    </div>
</div>
</form>
</
body>
</
html>

Ourput: Now Press F5 you will get the output as follows:

1.png

Figure 1: Show div by default.

Now click on the image, div will be close like as follows:

2.png

Figure 2: Hide the div

Login to add your contents and source code to this article
share this article :
post comment
 

Very good!!Thanks so much.

Posted by viet vo quoc Oct 14, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Team Foundation Server Hosting
Become a Sponsor