Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6211049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:16:08+00:00 2026-05-24T06:16:08+00:00

I have a collapsible div implemented in js, the function of which is show/hide

  • 0

I have a collapsible div implemented in js, the function of which is show/hide it’s child divs etc.

So when you click on it, it toggles the div’s disappear. Something similar to a tree view

What i want to do is that, i should be able to select the data in the child divs, but the problem is. when I click on the child divs the parent divs disappears as the onclick gets trigger i guess.

Please suggest how to should i achieve this. So my goal is to toggle a div’s display which enclosing child divs, but if you select something or highlight something in child div the parent div shouldn’t collapse etc.

Please see code below.

                        <html>
                <head>
                    <META http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
                    <title>Data</title>
                    <style>
                        #container {
                            clear: both;
                            width: 400px;
                            margin: 0 0;
                            border: none;
                        }

                        #row {
                            clear: both;
                            width: 100%;
                            border: none;
                        }

                        #column {
                            border: double #aca899;
                            float: left;
                            clear: none;
                            width: 402px;
                        }       

                        #columnHeader p {
                            float: left;
                            clear: none;
                            text-align: center;
                            width: 100%;
                            height: 20px;
                            padding-top: 2px;
                            font-size: 8.0pt;
                            font-family: Verdana;
                            font-weight: bold;
                            color: black;
                            text-decoration: underline;
                            border: 1px ridge #aca899;
                        }

                        #subSectionHeader {
                            float: left;
                            clear: none;
                            width: 50%;
                            font-size: 7.5pt;
                            font-family: Arial;
                            background-color: #DBA083;
                            border: 1px ridge #aca899;
                            border-collapse: collapse;
                            padding: 1px;
                        }       


                        #label {
                            float: left;
                            clear: none;
                            width: 50%;
                            font-size: 7.5pt;
                            font-family: Arial;
                            background-color: #F2CF93;
                            border: 1px ridge #aca899;
                            padding: 1px 1px 1px 11px;
                        }       


                        #Value {
                            float: left;
                            clear: none;
                            width: 50%;
                            font-size: 7.5pt;
                            font-family: Arial;
                            background-color: #DDDDDD;
                            border: 1px ridge #aca899;
                            padding: 1px;
                        }           

                    </style>
            <script type="text/javascript">
               function switchMenu(cur, e) {
               var obj = e.target || e.srcElement; //works on IE and FF
               var childDiv = cur.id + 'Child';


                    if (obj.parentNode.id != childDiv) {
                        var el = document.getElementById(childDiv);
                        if (el.style.display != "none") {
                            el.style.display = 'none';
                        }
                        else {
                            el.style.display = '';
                        }
                    }
                }

              </script>
                </head>

                <body>
                    <div id="container">

                        <div style="margin-top: 15px;"></div>
                        <div id="row">
                            <div id="column">
                            <div id="abc" onclick="switchMenu(this, event)">

                                <div id="columnHeader"><p>Section 1</p></div>                   

                                <div id="subSectionHeader"><p>Some Label I</p></div> <div id="subSectionHeader"><p>&nbsp;</p></div>
                                <div id="abcChild">
                                <div id="label"><p>Label</p></div>  <div id="value"><p>Value</p></div>                  
                                <div id="label"><p>Label</p></div>  <div id="value"><p>Value</p></div>                  
                                <div id="label"><p>Label</p></div>  <div id="value"><p>Value</p></div>                  
                                </div>
                                </div> <!--abc-->

                                <!--cde -->
                                <div id="cde" onclick="switchMenu(this, event)">
                                <div id="subSectionHeader"><p>Some Label II</p></div>   <div id="subSectionHeader"><p>&nbsp;</p></div>
                                <div id="cdeChild">
                                <div id="label"><p>Label</p></div>  <div id="value"><p>Value</p></div>
                                <div id="label"><p>Label</p></div>  <div id="value"><p>Value</p></div>
                                <div id="label"><p>Label</p></div>  <div id="value"><p>Value</p></div>
                                </div>
                                </div>
                                <!--cde -->
                            </div>  

                        </div>

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

Your suggestion and help will be appreciated
Thanks for your time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:16:09+00:00Added an answer on May 24, 2026 at 6:16 am

    Do not perform action if the click is performed on the child div.
    Update:

    The edited structure is different as you have p inside div. I’ve modified the code to work with the new structure. Note that id must be unique. Your css messes up the layout and causing the code to fail. The code works if you take out your css.

    This will be much simpler if you use jquery.

    Code updated to include suggestion by RobG

    Update2:
    Fixing bug in findParent and more robust.

    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <META http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
            <title>Data</title>
            <style>
                            #container {
                                clear: both;
                                width: 400px;
                                margin: 0 0;
                                border: none;
                            }
    
                            .row {
                                clear: both;
                                width: 100%;
                                border: none;
                            }
    
                            .column {
                                border: double #aca899;
                                float: left;
                                clear: none;
                                width: 402px;
                            }       
    
                            .columnHeader p {
                                float: left;
                                clear: none;
                                text-align: center;
                                width: 100%;
                                height: 20px;
                                padding-top: 2px;
                                font-size: 8.0pt;
                                font-family: Verdana;
                                font-weight: bold;
                                color: black;
                                text-decoration: underline;
                                border: 1px ridge #aca899;
                            }
    
                            .subSectionHeader {
                                float: left;
                                clear: none;
                                width: 50%;
                                font-size: 7.5pt;
                                font-family: Arial;
                                background-color: #DBA083;
                                border: 1px ridge #aca899;
                                border-collapse: collapse;
                                padding: 1px;
                            }       
    
    
                            .label {
                                float: left;
                                clear: none;
                                width: 50%;
                                font-size: 7.5pt;
                                font-family: Arial;
                                background-color: #F2CF93;
                                border: 1px ridge #aca899;
                                padding: 1px 1px 1px 11px;
                            }       
    
    
                            .value {
                                float: left;
                                clear: none;
                                width: 50%;
                                font-size: 7.5pt;
                                font-family: Arial;
                                background-color: #DDDDDD;
                                border: 1px ridge #aca899;
                                padding: 1px;
                            }          
            </style>
    <script type="text/javascript">
    function switchMenu(cur, e) {
        var obj = e.target || e.srcElement; //works on IE and FF
        var childDiv = cur.id + 'Child';
    
        if (obj.className.toLowerCase() != "nocollapse" && isInCollapsible(obj)) {
            var el = document.getElementById(childDiv);
            el.style.display = el.style.display != "none" ? "none" : "";
        }
    }
    
    function isInCollapsible(obj) {
        return findParent(obj, "div", "nocollapse") ? false : true;
    }
    
    function findParent(obj, type, name) {
        if (obj.parentNode)
            if (obj.parentNode.tagName && obj.parentNode.tagName.toLowerCase() == type && obj.parentNode.className.toLowerCase() == name)
                return obj.parentNode;
            else
                return findParent(obj.parentNode, type, name);
        else
            return null;
    }
    
        </script>
        </head>
    
        <body>
            <div id="container">
    
                <div style="margin-top: 15px;"></div>
                <div class="row">
                    <div class="column">
                    <div id="abc" onclick="switchMenu(this, event)">
    
                        <div class="columnHeader"><p>Section 1</p></div>                   
    
                        <div class="subSectionHeader"><p>Some Label I</p></div> <div class="subSectionHeader"><p>&nbsp;</p></div>
                        <div id="abcChild" class="noCollapse">
                        <div class="label"><p>Label</p></div>  <div class="value"><p>Value</p></div>                  
                        <div class="label"><p>Label</p></div>  <div class="value"><p>Value</p></div>                  
                        <div class="label"><p>Label</p></div>  <div class="value"><p>Value</p></div>                  
                        </div>
                        </div> <!--abc-->
    
                        <!--cde -->
                        <div id="cde" class="parent" onclick="switchMenu(this, event)">
                        <div class="subSectionHeader"><p>Some Label II</p></div>   <div class="subSectionHeader"><p>&nbsp;</p></div>
                        <div id="cdeChild" class="noCollapse">
                        <div class="label"><p>Label</p></div>  <div class="value"><p>Value</p></div>
                        <div class="label"><p>Label</p></div>  <div class="value"><p>Value</p></div>
                        <div class="label"><p>Label</p></div>  <div class="value"><p>Value</p></div>
                        </div>
                        </div>
                        <!--cde -->
                    </div>  
    
                </div>
    
            </div>
        </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below will show / hide a div on click. It works perfectly
I have the classical table with expandable and collapsible records that if expanded show
I have a set of divs that I want to make collapsible/expandable using jQuery's
I have some SQL output that id like to format into a collapsible div.
I'm using asp.net/c# and have a number of Collapsible Panel Extenders that have been
Have just started using Visual Studio Professional's built-in unit testing features, which as I
We have a collapsible tree implementation in our web app. I need to add
I have the following markup, I took the extra stuff out of the divs,
I have a page called Default.aspx which inherits from a master page called Main.master.
Is there a way I can have collapsible list and have the clicked list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.