[RESOLVED]How to Show/Hide only one div at a time?

I have several sections/DIVs that I want to display and only one div at a time. How do I accomplish that?

As for now every section / every DIV is displayed.

Here is the code in WebForm.aspx:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
    $(document).ready(function () {
        $("#show").click(function (e) {
            e.preventDefault();
            $("div").show();
        });
        $("#hide").click(function (e) {
            e.preventDefault();
            $("div").hide();
        });
    });
</script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Literal ID="Literal1" runat="server"></asp:Literal>
    </form>
</body>
</html>

And here is the code in WebForm.aspx.cs:

namespace WebServiceClient
{
    public partial class WebForm11 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();

            string[] sectionArray = new string[3] { "Section 1", "Section 2", "Section 3" };

            for (var section = 0; section < sectionArray.Length; section++)
            {
            sb.Append("<button id='show'>Show</button>");
            sb.Append("<button id='hide'>Hide</button>");

            sb.Append("<div style='display:none'>"); // Content to show/hide
            sb.Append(sectionArray[section]);
            sb.Append("</div>");

            sb.Append("<hr />");

            Literal1.Text = sb.ToString();
            }

        }
    }
}

So how can I show only one DIV/Section at a time? Not all at the same time.

You are trying to use the element selector in jquery and it will select all element with given tagname(div). You need to differentiate the div based on the button clicked an then use the jquery ID selector to select each div.

Sample Implemenation:

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            $("#show").click(function (e) {
                e.preventDefault();
              //  $("div").show();
                $('#' + this.name).show();
            });
            $("#hide").click(function (e) {
                e.preventDefault();
                // $("div").hide();
                $('#' + this.name).hide();
            });
        });
    </script>
    <asp:Literal ID="Literal1" runat="server"></asp:Literal>

C#:

 StringBuilder sb = new StringBuilder();

                string[] sectionArray = new string[3] { "Section 1", "Section 2", "Section 3" };

                for (var section = 0; section < sectionArray.Length; section++)
                {
                    sb.Append("<button name='" + section + "' id='show'>Show</button>");
                    sb.Append("<button name='" + section + "' id='hide'>Hide</button>");

                    sb.Append("<div id='" + section + "' style='display:none'>"); // Content to show/hide
                    sb.Append(sectionArray[section]);
                    sb.Append("</div>");

                    sb.Append("<hr />");

                    Literal1.Text = sb.ToString();
                }

Ptolemy

I have several sections/DIVs that I want to display and only one div at a time. How do I accomplish that?

Hi Ptolemy,

You can try to use the jquery ui according to complete your feature like the example below.

<head>

  <meta charset="utf-8">

  <title>jQuery UI Accordion - Default functionality</title>

  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css">

  <script src="//code.jquery.com/jquery-1.10.2.js"></script>

  <script src="//code.jquery.com/ui/1.11.1/jquery-ui.js"></script>

  <link rel="stylesheet" href="/resources/demos/style.css">

  <script>

  $(function() {

    $( "#accordion" ).accordion();

  });

  </script>

</head>

<body>

 

<div id="accordion">

  <h3>Section 1</h3>

  <div>

    <p>

    Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer

    ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit

    amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut

    odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.

    </p>

  </div>

  <h3>Section 2</h3>

  <div>

    <p>

    Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet

    purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor

    velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In

    suscipit faucibus urna.

    </p>

  </div>

  <h3>Section 3</h3>

  <div>

    <p>

    Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis.

    Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero

    ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis

    lacinia ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui.

    </p>

    <ul>

      <li>List item one</li>

      <li>List item two</li>

      <li>List item three</li>

    </ul>

  </div>

  <h3>Section 4</h3>

  <div>

    <p>

    Cras dictum. Pellentesque habitant morbi tristique senectus et netus

    et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in

    faucibus orci luctus et ultrices posuere cubilia Curae; Aenean lacinia

    mauris vel est.

    </p>

    <p>

    Suspendisse eu nisl. Nullam ut libero. Integer dignissim consequat lectus.

    Class aptent taciti sociosqu ad litora torquent per conubia nostra, per

    inceptos himenaeos.

    </p>

  </div>

</div>
</body>

More information you can follow below

https://jqueryui.com/accordion/

Thanks.

Best Regards!

A2H

You are trying to use the element selector in jquery and it will select all element with given tagname(div). You need to differentiate the div based on the button clicked an then use the jquery ID selector to select each div.

Sample Implemenation:

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            $("#show").click(function (e) {
                e.preventDefault();
              //  $("div").show();
                $('#' + this.name).show();
            });
            $("#hide").click(function (e) {
                e.preventDefault();
                // $("div").hide();
                $('#' + this.name).hide();
            });
        });
    </script>
    <asp:Literal ID="Literal1" runat="server"></asp:Literal>

C#:

 StringBuilder sb = new StringBuilder();

                string[] sectionArray = new string[3] { "Section 1", "Section 2", "Section 3" };

                for (var section = 0; section < sectionArray.Length; section++)
                {
                    sb.Append("<button name='" + section + "' id='show'>Show</button>");
                    sb.Append("<button name='" + section + "' id='hide'>Hide</button>");

                    sb.Append("<div id='" + section + "' style='display:none'>"); // Content to show/hide
                    sb.Append(sectionArray[section]);
                    sb.Append("</div>");

                    sb.Append("<hr />");

                    Literal1.Text = sb.ToString();
                }

Thanks for your help!

Now all divs doesn’t open at the same time so at least some progress.  But only the first div opens when clicking on the button for section 1. When I click on buttons related to section 2 and 3 nothing happens. I don’t know why because your code does seem
to make sense.

What could be the problem?

I found another solution that worked very well:

http://andylangton.co.uk/blog/development/accessible-showhide-content-jquery

But anyway I want to thank both of you for your feedback.

I’m not really sure about how this ui accordion thing would work, but might look into that deeper later on.

Leave a Reply