/    Sign up×
Community /Pin to ProfileBookmark

Select Accordion Parent

I am trying to do an accordion when the upper level i selected. I can easily select the panel inside the accordion, but how should I make the upper level stay selected?

https://jsfiddle.net/w4mv5hLb/20/

TIA,

to post a comment
CSSJavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@JMRKEROct 19.2019 — Make the following changes to your code.

Add 'open' to the class of the first DIV

Note modification for 2 options in the SCRIPT section.
<i>
</i>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="UTF-8"&gt;
&lt;meta name="viewport" content="width-device-width,initial-scale=1.0, user-scalable=yes"/&gt;
&lt;title&gt; Accordion Menu &lt;/title&gt;
&lt;!-- From: https://jsfiddle.net/w4mv5hLb/20/ --&gt;
&lt;style&gt;
.accordion {
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
}
.accordion:hover {
background-color: #900;
}
.panel {
padding: 10px;
display: none;
background-color: white;
overflow: hidden;
}
.accordion.open {
background-color: #900;
}
.panel.open {
display: block;
}
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;button class="accordion"&gt;Section 1&lt;/button&gt;
&lt;div class="panel open"&gt;
&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.&lt;/p&gt;
&lt;/div&gt;

<i> </i>&lt;button class="accordion"&gt;Section 2&lt;/button&gt;
<i> </i>&lt;div class="panel"&gt;
<i> </i> &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
<i> </i> Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.&lt;/p&gt;
<i> </i>&lt;/div&gt;

<i> </i>&lt;button class="accordion"&gt;Section 3&lt;/button&gt;
<i> </i>&lt;div class="panel"&gt;
<i> </i> &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
<i> </i> Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.&lt;/p&gt;
<i> </i>&lt;/div&gt;


&lt;script&gt;
const acc = document.querySelectorAll('.accordion');

acc.forEach(
function(elem) {
elem.addEventListener('click', function(evt) {
/* Modify as follows or remove altogether
acc.forEach(function(elem) {
if (elem != acc[0]) { elem.nextElementSibling.classList.remove("open") };
});
/* */
var panel = evt.target.nextElementSibling;
if (elem != acc[0]) { panel.classList.toggle('open'); }
}, true);
}
);

&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;

:) Good Luck!
Copy linkTweet thisAlerts:
@sibertauthorOct 20.2019 — > @JMRKER#1610204 Make the following changes to your code.

> Add 'open' to the class of the first DIV


Thanks, but not quite my goal.

  • 1. Only ONE accordion should stay open. Automatically close other.

  • 2. The selected accordion should stay selected (red). Not only when hover.


  • Any further tip?
    Copy linkTweet thisAlerts:
    @sibertauthorOct 20.2019 — I found one way to do this:

    https://jsfiddle.net/1rsb7gmo/5/

    Can it be done simpler or better?
    Copy linkTweet thisAlerts:
    @JMRKEROct 20.2019 — @sibert#1610215

    Will try one more time...

    <i>
    </i>&lt;!DOCTYPE html&gt;
    &lt;html lang="en"&gt;
    &lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;meta name="viewport" content="width-device-width,initial-scale=1.0, user-scalable=yes"/&gt;
    &lt;title&gt; Accordion Panels &lt;/title&gt;
    &lt;!-- Modified From: http://w3schools.invisionzone.com/topic/60248-link-to-open-accordion/ --&gt;

    &lt;style&gt;
    .accordion {
    background: none;
    color: black;
    cursor: pointer;
    padding: 0.5em;
    width: 50%;
    text-align: left;
    font-size: 1em;
    font-weight: bold;
    transition: 0.4s;
    }
    .accordion:after {
    content: '02B';
    color: #2e6885;
    font-weight: bold;
    float: right;
    margin-left: 0.25em;
    }
    .accordion:hover {
    background: lime; /* color: black; */
    }

    .mark { background: lime; }
    .mark:after { content: "2212"; }
    .hide { display: none; }

    .panel {
    padding: 0 1em;
    background-color: white;
    transition: max-height 0.2s ease-out;
    }
    &lt;/style&gt;

    &lt;/head&gt;
    &lt;body&gt;
    &lt;button class="accordion"&gt;TITLE 1&lt;/button&gt;
    &lt;div class="panel hide"&gt; &lt;p&gt; CONTENT&lt;/p&gt; &lt;/div&gt;

    &lt;button class="accordion"&gt;TITLE 2&lt;/button&gt;
    &lt;div class="panel hide"&gt; &lt;p&gt;CONTENT&lt;/p&gt; &lt;/div&gt;

    &lt;button class="accordion"&gt;TITLE 3&lt;/button&gt;
    &lt;div class="panel hide"&gt; &lt;p&gt;CONTENT&lt;/p&gt; &lt;/div&gt;

    &lt;script&gt;
    console.clear();
    const doc = (IDS) =&gt; document.getElementById(IDS);

    var sel = document.querySelectorAll('.accordion'),
    pan = document.querySelectorAll('.panel');
    for (const [i, el] of sel.entries()) {
    el.addEventListener('click', function() { test(el, i); } );
    }

    function test(info, ndx) {
    const selMatch = [...sel].filter( (elem) =&gt; { // expressions that return 'false' are retained
    return info != elem;
    });
    selMatch.forEach( x =&gt; { x.classList.remove('mark');
    x.nextElementSibling.classList.add('hide') } );

    <i> </i>info.classList.toggle('mark');
    <i> </i>if ( info.classList.contains('mark')) { pan[ndx].classList.remove('hide'); }
    <i> </i> else { pan[ndx].classList.add('hide'); }
    }

    sel[0].click();
    &lt;/script&gt;

    &lt;/body&gt;
    &lt;/html&gt;
    Copy linkTweet thisAlerts:
    @sibertauthorOct 21.2019 — > @JMRKER#1610224 Will try one more time...

    Thanks! I do not find it simpler, but sure better than mine:

    https://jsfiddle.net/2gwxpo1e/

    Is this code necessary?

    const doc = (IDS) =&gt; document.getElementById(IDS);
    Copy linkTweet thisAlerts:
    @JMRKEROct 21.2019 — @sibert#1610228

    Nope, not needed for this program. Got left behind as I use it often in other projects.
    Copy linkTweet thisAlerts:
    @siddhi_patelNov 07.2019 — Hi u can use like this :
    ``<i>
    </i>&lt;div id="accordion"&gt;

    &lt;div class="card"&gt;
    &lt;div class="card-header"&gt;
    &lt;a class="card-link" data-toggle="collapse" href="#collapseOne"&gt;
    Collapsible Group Item #1
    &lt;/a&gt;
    &lt;/div&gt;
    &lt;div id="collapseOne" class="collapse show" data-parent="#accordion"&gt;
    &lt;div class="card-body"&gt;
    Lorem ipsum..
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;/div&gt;

    &lt;div class="card"&gt;
    &lt;div class="card-header"&gt;
    &lt;a class="collapsed card-link" data-toggle="collapse" href="#collapseTwo"&gt;
    Collapsible Group Item #2
    &lt;/a&gt;
    &lt;/div&gt;
    &lt;div id="collapseTwo" class="collapse" data-parent="#accordion"&gt;
    &lt;div class="card-body"&gt;
    Lorem ipsum..
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;/div&gt;

    &lt;div class="card"&gt;
    &lt;div class="card-header"&gt;
    &lt;a class="collapsed card-link" data-toggle="collapse" href="#collapseThree"&gt;
    Collapsible Group Item #3
    &lt;/a&gt;
    &lt;/div&gt;
    &lt;div id="collapseThree" class="collapse" data-parent="#accordion"&gt;
    &lt;div class="card-body"&gt;
    Lorem ipsum..
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;/div&gt;<i>
    </i>
    ``
    Copy linkTweet thisAlerts:
    @JMRKERNov 08.2019 — @siddhi_patel#1610707

    That last script does nothing on it's own anywhere close to an accordion display.

    Which CSS statements are you using?
    Copy linkTweet thisAlerts:
    @siddhi_patelNov 14.2019 — @JMRKER#1610750

    You have to use bootstrap library file into your project.

    Following is the style link... or you can download from w3school.com
    ``<i>
    </i> &lt;link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"&gt;
    &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"&gt;&lt;/script&gt;
    &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"&gt;&lt;/script&gt;
    &lt;script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"&gt;&lt;/script&gt;<i>
    </i>
    ``
    Copy linkTweet thisAlerts:
    @JMRKERNov 15.2019 — Sorry, I don't have to use a bootstrap library. Never have and have no desire.

    Not my project. I wanted to help original post author, which I did without any

    extra libraries in the earlier posts. You are welcome to your opinion and I'll

    let the original author decide if they wish to incorporate your suggestion.
    ×

    Success!

    Help @sibert spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.19,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...