Close all open drop downs and consolidate code if possibleChange the selected value of a drop-down list with jQueryGet selected text from a drop-down list (select box) using jQueryHTML href with css ie ProblemjQuery draggable + toggleClass problemDynamically loaded CSS having no effect in a Windows 8 appBootstrap 3 - background-color issueResponsive navigation menu Toggle'nSome problems with PADDING in bg color of ONLY the inline text elementToggle multiple divs with multiple buttons using animate.css - jQueryUnwanted overlay of java script picture slide animation with website header

How to respond to an upset student?

How can I tell if I'm being too picky as a referee?

Is it possible to build VPN remote access environment without VPN server?

Looking for a soft substance that doesn't dissolve underwater

What is the largest (size) solid object ever dropped from an airplane to impact the ground in freefall?

How to execute this code on startup?

Why doesn't the Earth accelerate towards the Moon?

How to illustrate the Mean Value theorem?

Why colon to denote that a value belongs to a type?

US Passport stamped in UK for 6 months twice

Is the Starlink array really visible from Earth?

Where is the logic in castrating fighters?

Should breaking down something like a door be adjudicated as an attempt to beat its AC and HP, or as an ability check against a set DC?

I think I may have violated academic integrity last year - what should I do?

Installed Electric Tankless Water Heater - Internet loss when active

How strong are Wi-Fi signals?

Why aren't space telescopes put in GEO?

Were pens caps holes designed to prevent death by suffocation if swallowed?

Pirate democracy at its finest

the meaning of 'carry' in a novel

Is the Indo-European language family made up?

Defining the standard model of PA so that a space alien could understand

Boss wants me to falsify a report. How should I document this unethical demand?

Simple function that simulates survey results based on sample size and probability



Close all open drop downs and consolidate code if possible


Change the selected value of a drop-down list with jQueryGet selected text from a drop-down list (select box) using jQueryHTML href with css ie ProblemjQuery draggable + toggleClass problemDynamically loaded CSS having no effect in a Windows 8 appBootstrap 3 - background-color issueResponsive navigation menu Toggle'nSome problems with PADDING in bg color of ONLY the inline text elementToggle multiple divs with multiple buttons using animate.css - jQueryUnwanted overlay of java script picture slide animation with website header






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















What I need to be able to do is to have the text drop downs close when another is selected so that I do not end up with a bunch of drop downs open on the page at the same time.



I have two text dropdowns that will be used one after the other alternating on a page. In other words accordion1, accordion2, accordion1, accordion2 and so on the reason I have accordion1 and accordion2 is that with my limited experience it is the only way I could figure out change the button color so the list could alternate colors. It would be nice to consolidate the code, but I can live with the extra code if need be.



Here is the code for accordion1






var acc = document.getElementsByClassName("accordion1");
var i;

for (i = 0; i < acc.length; i++)
acc[i].addEventListener("click", function()
/* Toggle between adding and removing the "active" class,
to highlight the button that controls the panel */
this.classList.toggle("active1");

/* Toggle between hiding and showing the active panel */
var panel = this.nextElementSibling;
if (panel.style.display === "block")
panel.style.display = "none";
else
panel.style.display = "block";

);


var acc = document.getElementsByClassName("accordion2");
var i;

for (i = 0; i < acc.length; i++)
acc[i].addEventListener("click", function()
/* Toggle between adding and removing the "active" class,
to highlight the button that controls the panel */
this.classList.toggle("active1");

/* Toggle between hiding and showing the active panel */
var panel = this.nextElementSibling;
if (panel.style.display === "block")
panel.style.display = "none";
else
panel.style.display = "block";

);

.accordion1 
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
text-align: left;
border: none;
outline: none;
transition: 0.4s;


.accordion2
background-color: #8461E8;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
text-align: left;
border: none;
outline: none;
transition: 0.4s;



/* Add a background color to the button if it is clicked on (add the
.active class with JS), and when you move the mouse over it (hover) */

.active1,
.accordion1:hover
background-color: #ccc;



/* Style the accordion panel. Note: hidden by default */

.panel1
padding: 0 18px;
background-color: white;
display: none;
overflow: hidden;


.accordion1:after
content: '2795';
/* Unicode character for "plus" sign (+) */
font-size: 13px;
color: #777;
float: right;
margin-left: 5px;


.accordion2:after
content: '2795';
/* Unicode character for "plus" sign (+) */
font-size: 13px;
color: #777;
float: right;
margin-left: 5px;


.active1:after
content: "2796";
/* Unicode character for "minus" sign (-) */

<Section><button class="accordion1"><h3>Alternators and regulators</h3> 
</button>
<div id="accordion1-div" class="panel1">
<p>First group of words go here></div>
</Section>

<Section><button class="accordion2"><h3>Batteries and Inverters</h3>
</button>
<div id="accordion-div" class="panel1">
<p>Second set of words go here.</p>
</div>
</Section>

<Section><button class="accordion1"><h3>AC and DC Panels </h3>
</button>
<div id="accordian1-div" class="panel1">
<p>Third set of words reusing "accordion1 go here"</p>
</div>
</Section>





Any help or resources to figure out the needed code would be greatly appreciated.










share|improve this question















migrated from serverfault.com May 13 at 17:41


This question came from our site for system and network administrators.


















  • You can use :even and :odd css rules to handle the alternating colors. You can also have more than one class per element so there is no need for the duplicated accordion script. What if you just keep a reference to the currently opened element? Every time a new element is clicked, remove the active class from the currently opened element and then store the newly click element. If you post some html, we can help you refine your js.

    – JasonB
    May 13 at 21:54











  • JasonB ... Thank you for responding .. html added

    – Randy Morgan
    May 13 at 22:36

















0















What I need to be able to do is to have the text drop downs close when another is selected so that I do not end up with a bunch of drop downs open on the page at the same time.



I have two text dropdowns that will be used one after the other alternating on a page. In other words accordion1, accordion2, accordion1, accordion2 and so on the reason I have accordion1 and accordion2 is that with my limited experience it is the only way I could figure out change the button color so the list could alternate colors. It would be nice to consolidate the code, but I can live with the extra code if need be.



Here is the code for accordion1






var acc = document.getElementsByClassName("accordion1");
var i;

for (i = 0; i < acc.length; i++)
acc[i].addEventListener("click", function()
/* Toggle between adding and removing the "active" class,
to highlight the button that controls the panel */
this.classList.toggle("active1");

/* Toggle between hiding and showing the active panel */
var panel = this.nextElementSibling;
if (panel.style.display === "block")
panel.style.display = "none";
else
panel.style.display = "block";

);


var acc = document.getElementsByClassName("accordion2");
var i;

for (i = 0; i < acc.length; i++)
acc[i].addEventListener("click", function()
/* Toggle between adding and removing the "active" class,
to highlight the button that controls the panel */
this.classList.toggle("active1");

/* Toggle between hiding and showing the active panel */
var panel = this.nextElementSibling;
if (panel.style.display === "block")
panel.style.display = "none";
else
panel.style.display = "block";

);

.accordion1 
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
text-align: left;
border: none;
outline: none;
transition: 0.4s;


.accordion2
background-color: #8461E8;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
text-align: left;
border: none;
outline: none;
transition: 0.4s;



/* Add a background color to the button if it is clicked on (add the
.active class with JS), and when you move the mouse over it (hover) */

.active1,
.accordion1:hover
background-color: #ccc;



/* Style the accordion panel. Note: hidden by default */

.panel1
padding: 0 18px;
background-color: white;
display: none;
overflow: hidden;


.accordion1:after
content: '2795';
/* Unicode character for "plus" sign (+) */
font-size: 13px;
color: #777;
float: right;
margin-left: 5px;


.accordion2:after
content: '2795';
/* Unicode character for "plus" sign (+) */
font-size: 13px;
color: #777;
float: right;
margin-left: 5px;


.active1:after
content: "2796";
/* Unicode character for "minus" sign (-) */

<Section><button class="accordion1"><h3>Alternators and regulators</h3> 
</button>
<div id="accordion1-div" class="panel1">
<p>First group of words go here></div>
</Section>

<Section><button class="accordion2"><h3>Batteries and Inverters</h3>
</button>
<div id="accordion-div" class="panel1">
<p>Second set of words go here.</p>
</div>
</Section>

<Section><button class="accordion1"><h3>AC and DC Panels </h3>
</button>
<div id="accordian1-div" class="panel1">
<p>Third set of words reusing "accordion1 go here"</p>
</div>
</Section>





Any help or resources to figure out the needed code would be greatly appreciated.










share|improve this question















migrated from serverfault.com May 13 at 17:41


This question came from our site for system and network administrators.


















  • You can use :even and :odd css rules to handle the alternating colors. You can also have more than one class per element so there is no need for the duplicated accordion script. What if you just keep a reference to the currently opened element? Every time a new element is clicked, remove the active class from the currently opened element and then store the newly click element. If you post some html, we can help you refine your js.

    – JasonB
    May 13 at 21:54











  • JasonB ... Thank you for responding .. html added

    – Randy Morgan
    May 13 at 22:36













0












0








0








What I need to be able to do is to have the text drop downs close when another is selected so that I do not end up with a bunch of drop downs open on the page at the same time.



I have two text dropdowns that will be used one after the other alternating on a page. In other words accordion1, accordion2, accordion1, accordion2 and so on the reason I have accordion1 and accordion2 is that with my limited experience it is the only way I could figure out change the button color so the list could alternate colors. It would be nice to consolidate the code, but I can live with the extra code if need be.



Here is the code for accordion1






var acc = document.getElementsByClassName("accordion1");
var i;

for (i = 0; i < acc.length; i++)
acc[i].addEventListener("click", function()
/* Toggle between adding and removing the "active" class,
to highlight the button that controls the panel */
this.classList.toggle("active1");

/* Toggle between hiding and showing the active panel */
var panel = this.nextElementSibling;
if (panel.style.display === "block")
panel.style.display = "none";
else
panel.style.display = "block";

);


var acc = document.getElementsByClassName("accordion2");
var i;

for (i = 0; i < acc.length; i++)
acc[i].addEventListener("click", function()
/* Toggle between adding and removing the "active" class,
to highlight the button that controls the panel */
this.classList.toggle("active1");

/* Toggle between hiding and showing the active panel */
var panel = this.nextElementSibling;
if (panel.style.display === "block")
panel.style.display = "none";
else
panel.style.display = "block";

);

.accordion1 
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
text-align: left;
border: none;
outline: none;
transition: 0.4s;


.accordion2
background-color: #8461E8;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
text-align: left;
border: none;
outline: none;
transition: 0.4s;



/* Add a background color to the button if it is clicked on (add the
.active class with JS), and when you move the mouse over it (hover) */

.active1,
.accordion1:hover
background-color: #ccc;



/* Style the accordion panel. Note: hidden by default */

.panel1
padding: 0 18px;
background-color: white;
display: none;
overflow: hidden;


.accordion1:after
content: '2795';
/* Unicode character for "plus" sign (+) */
font-size: 13px;
color: #777;
float: right;
margin-left: 5px;


.accordion2:after
content: '2795';
/* Unicode character for "plus" sign (+) */
font-size: 13px;
color: #777;
float: right;
margin-left: 5px;


.active1:after
content: "2796";
/* Unicode character for "minus" sign (-) */

<Section><button class="accordion1"><h3>Alternators and regulators</h3> 
</button>
<div id="accordion1-div" class="panel1">
<p>First group of words go here></div>
</Section>

<Section><button class="accordion2"><h3>Batteries and Inverters</h3>
</button>
<div id="accordion-div" class="panel1">
<p>Second set of words go here.</p>
</div>
</Section>

<Section><button class="accordion1"><h3>AC and DC Panels </h3>
</button>
<div id="accordian1-div" class="panel1">
<p>Third set of words reusing "accordion1 go here"</p>
</div>
</Section>





Any help or resources to figure out the needed code would be greatly appreciated.










share|improve this question
















What I need to be able to do is to have the text drop downs close when another is selected so that I do not end up with a bunch of drop downs open on the page at the same time.



I have two text dropdowns that will be used one after the other alternating on a page. In other words accordion1, accordion2, accordion1, accordion2 and so on the reason I have accordion1 and accordion2 is that with my limited experience it is the only way I could figure out change the button color so the list could alternate colors. It would be nice to consolidate the code, but I can live with the extra code if need be.



Here is the code for accordion1






var acc = document.getElementsByClassName("accordion1");
var i;

for (i = 0; i < acc.length; i++)
acc[i].addEventListener("click", function()
/* Toggle between adding and removing the "active" class,
to highlight the button that controls the panel */
this.classList.toggle("active1");

/* Toggle between hiding and showing the active panel */
var panel = this.nextElementSibling;
if (panel.style.display === "block")
panel.style.display = "none";
else
panel.style.display = "block";

);


var acc = document.getElementsByClassName("accordion2");
var i;

for (i = 0; i < acc.length; i++)
acc[i].addEventListener("click", function()
/* Toggle between adding and removing the "active" class,
to highlight the button that controls the panel */
this.classList.toggle("active1");

/* Toggle between hiding and showing the active panel */
var panel = this.nextElementSibling;
if (panel.style.display === "block")
panel.style.display = "none";
else
panel.style.display = "block";

);

.accordion1 
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
text-align: left;
border: none;
outline: none;
transition: 0.4s;


.accordion2
background-color: #8461E8;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
text-align: left;
border: none;
outline: none;
transition: 0.4s;



/* Add a background color to the button if it is clicked on (add the
.active class with JS), and when you move the mouse over it (hover) */

.active1,
.accordion1:hover
background-color: #ccc;



/* Style the accordion panel. Note: hidden by default */

.panel1
padding: 0 18px;
background-color: white;
display: none;
overflow: hidden;


.accordion1:after
content: '2795';
/* Unicode character for "plus" sign (+) */
font-size: 13px;
color: #777;
float: right;
margin-left: 5px;


.accordion2:after
content: '2795';
/* Unicode character for "plus" sign (+) */
font-size: 13px;
color: #777;
float: right;
margin-left: 5px;


.active1:after
content: "2796";
/* Unicode character for "minus" sign (-) */

<Section><button class="accordion1"><h3>Alternators and regulators</h3> 
</button>
<div id="accordion1-div" class="panel1">
<p>First group of words go here></div>
</Section>

<Section><button class="accordion2"><h3>Batteries and Inverters</h3>
</button>
<div id="accordion-div" class="panel1">
<p>Second set of words go here.</p>
</div>
</Section>

<Section><button class="accordion1"><h3>AC and DC Panels </h3>
</button>
<div id="accordian1-div" class="panel1">
<p>Third set of words reusing "accordion1 go here"</p>
</div>
</Section>





Any help or resources to figure out the needed code would be greatly appreciated.






var acc = document.getElementsByClassName("accordion1");
var i;

for (i = 0; i < acc.length; i++)
acc[i].addEventListener("click", function()
/* Toggle between adding and removing the "active" class,
to highlight the button that controls the panel */
this.classList.toggle("active1");

/* Toggle between hiding and showing the active panel */
var panel = this.nextElementSibling;
if (panel.style.display === "block")
panel.style.display = "none";
else
panel.style.display = "block";

);


var acc = document.getElementsByClassName("accordion2");
var i;

for (i = 0; i < acc.length; i++)
acc[i].addEventListener("click", function()
/* Toggle between adding and removing the "active" class,
to highlight the button that controls the panel */
this.classList.toggle("active1");

/* Toggle between hiding and showing the active panel */
var panel = this.nextElementSibling;
if (panel.style.display === "block")
panel.style.display = "none";
else
panel.style.display = "block";

);

.accordion1 
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
text-align: left;
border: none;
outline: none;
transition: 0.4s;


.accordion2
background-color: #8461E8;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
text-align: left;
border: none;
outline: none;
transition: 0.4s;



/* Add a background color to the button if it is clicked on (add the
.active class with JS), and when you move the mouse over it (hover) */

.active1,
.accordion1:hover
background-color: #ccc;



/* Style the accordion panel. Note: hidden by default */

.panel1
padding: 0 18px;
background-color: white;
display: none;
overflow: hidden;


.accordion1:after
content: '2795';
/* Unicode character for "plus" sign (+) */
font-size: 13px;
color: #777;
float: right;
margin-left: 5px;


.accordion2:after
content: '2795';
/* Unicode character for "plus" sign (+) */
font-size: 13px;
color: #777;
float: right;
margin-left: 5px;


.active1:after
content: "2796";
/* Unicode character for "minus" sign (-) */

<Section><button class="accordion1"><h3>Alternators and regulators</h3> 
</button>
<div id="accordion1-div" class="panel1">
<p>First group of words go here></div>
</Section>

<Section><button class="accordion2"><h3>Batteries and Inverters</h3>
</button>
<div id="accordion-div" class="panel1">
<p>Second set of words go here.</p>
</div>
</Section>

<Section><button class="accordion1"><h3>AC and DC Panels </h3>
</button>
<div id="accordian1-div" class="panel1">
<p>Third set of words reusing "accordion1 go here"</p>
</div>
</Section>





var acc = document.getElementsByClassName("accordion1");
var i;

for (i = 0; i < acc.length; i++)
acc[i].addEventListener("click", function()
/* Toggle between adding and removing the "active" class,
to highlight the button that controls the panel */
this.classList.toggle("active1");

/* Toggle between hiding and showing the active panel */
var panel = this.nextElementSibling;
if (panel.style.display === "block")
panel.style.display = "none";
else
panel.style.display = "block";

);


var acc = document.getElementsByClassName("accordion2");
var i;

for (i = 0; i < acc.length; i++)
acc[i].addEventListener("click", function()
/* Toggle between adding and removing the "active" class,
to highlight the button that controls the panel */
this.classList.toggle("active1");

/* Toggle between hiding and showing the active panel */
var panel = this.nextElementSibling;
if (panel.style.display === "block")
panel.style.display = "none";
else
panel.style.display = "block";

);

.accordion1 
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
text-align: left;
border: none;
outline: none;
transition: 0.4s;


.accordion2
background-color: #8461E8;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
text-align: left;
border: none;
outline: none;
transition: 0.4s;



/* Add a background color to the button if it is clicked on (add the
.active class with JS), and when you move the mouse over it (hover) */

.active1,
.accordion1:hover
background-color: #ccc;



/* Style the accordion panel. Note: hidden by default */

.panel1
padding: 0 18px;
background-color: white;
display: none;
overflow: hidden;


.accordion1:after
content: '2795';
/* Unicode character for "plus" sign (+) */
font-size: 13px;
color: #777;
float: right;
margin-left: 5px;


.accordion2:after
content: '2795';
/* Unicode character for "plus" sign (+) */
font-size: 13px;
color: #777;
float: right;
margin-left: 5px;


.active1:after
content: "2796";
/* Unicode character for "minus" sign (-) */

<Section><button class="accordion1"><h3>Alternators and regulators</h3> 
</button>
<div id="accordion1-div" class="panel1">
<p>First group of words go here></div>
</Section>

<Section><button class="accordion2"><h3>Batteries and Inverters</h3>
</button>
<div id="accordion-div" class="panel1">
<p>Second set of words go here.</p>
</div>
</Section>

<Section><button class="accordion1"><h3>AC and DC Panels </h3>
</button>
<div id="accordian1-div" class="panel1">
<p>Third set of words reusing "accordion1 go here"</p>
</div>
</Section>






javascript css jquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 14 at 1:30









Barthy

1,5111131




1,5111131










asked May 13 at 17:10









Randy MorganRandy Morgan

34




34




migrated from serverfault.com May 13 at 17:41


This question came from our site for system and network administrators.









migrated from serverfault.com May 13 at 17:41


This question came from our site for system and network administrators.














  • You can use :even and :odd css rules to handle the alternating colors. You can also have more than one class per element so there is no need for the duplicated accordion script. What if you just keep a reference to the currently opened element? Every time a new element is clicked, remove the active class from the currently opened element and then store the newly click element. If you post some html, we can help you refine your js.

    – JasonB
    May 13 at 21:54











  • JasonB ... Thank you for responding .. html added

    – Randy Morgan
    May 13 at 22:36

















  • You can use :even and :odd css rules to handle the alternating colors. You can also have more than one class per element so there is no need for the duplicated accordion script. What if you just keep a reference to the currently opened element? Every time a new element is clicked, remove the active class from the currently opened element and then store the newly click element. If you post some html, we can help you refine your js.

    – JasonB
    May 13 at 21:54











  • JasonB ... Thank you for responding .. html added

    – Randy Morgan
    May 13 at 22:36
















You can use :even and :odd css rules to handle the alternating colors. You can also have more than one class per element so there is no need for the duplicated accordion script. What if you just keep a reference to the currently opened element? Every time a new element is clicked, remove the active class from the currently opened element and then store the newly click element. If you post some html, we can help you refine your js.

– JasonB
May 13 at 21:54





You can use :even and :odd css rules to handle the alternating colors. You can also have more than one class per element so there is no need for the duplicated accordion script. What if you just keep a reference to the currently opened element? Every time a new element is clicked, remove the active class from the currently opened element and then store the newly click element. If you post some html, we can help you refine your js.

– JasonB
May 13 at 21:54













JasonB ... Thank you for responding .. html added

– Randy Morgan
May 13 at 22:36





JasonB ... Thank you for responding .. html added

– Randy Morgan
May 13 at 22:36












1 Answer
1






active

oldest

votes


















2














Question 1 — "How do I not end up with a bunch of drop downs open on the page at the same time":



You close all dropdowns before opening another one. You can also create css rules to display or hide the dropdown. This way, it will be easier to find the currently active dropdown. See code below.



Question 2 — "How can I make the list alternate colors"



You can add more than one class to an element. Simply create color classes and add them to the right elements. See code below.



Notes:



  • Use the CSS selectors instead of JavaScript to show/hide the panel


  • Element h3 is not allowed as child of element button. Do it the other way round.

  • Use the same JavaScript code and CSS for all accordions.

Edit (scrollIntoView)



I added code to automatically scroll the window so that the active tab is visible.
It works only on Chrome, Firefox and Opera. Use this polyfill iamdustan/smoothscroll to use it in other browsers. See compatibility here and all functions here.






// Query all accordions
var accordions = document.querySelectorAll('.accordion');

for (var i = 0; i < accordions.length; i++)
accordions[i].addEventListener('click', function()
// Get the currently active accordion
var active = document.querySelector('.accordion.active');

// If there is one, deactivate it
if (active)
active.classList.remove('active');


// Activate the new accordion, if it's not the one you just deactivated
if (active !== this)
this.classList.add('active');

// Use scrollIntoView to adjust the window scroll position to show the content.
this.scrollIntoView(
behavior: 'smooth'
);

);

.accordion .header button 
text-align: left;
padding: 18px;
background: transparent;
border: none;
outline: none;
cursor: pointer;
width: 100%;
color: #444;
width: 100%;
transition: 0.4s;



/* Set the color according to the modifier class */

.accordion.gray button
background-color: #eee;


.accordion.purple button
background-color: #8461E8;


.accordion.active button,
.accordion button:hover
background-color: #ccc;


.accordion .panel
padding: 0 18px;
background-color: white;
display: none;
overflow: hidden;



/* Show the panel if the accordion is active */

.accordion.active .panel
display: block;


.accordion button:after
content: '2795';
font-size: 13px;
color: #777;
float: right;
margin-left: 5px;


.accordion.active button:after
content: "2796";

<section>
<!-- Each accordion is wrapped by a div with the class 'accordion' -->
<!-- 'accordion' is the component, 'gray' is the color modifier -->
<div class="accordion gray">
<!-- h3 can contain a button -->
<h3 class="header">
<button>Alternators and regulators</button>
</h3>
<div class="panel">
<p>
First group of words go here.<br/> I'm afraid I just blue myself. No… but I'd like to be asked! Michael! It's called 'taking advantage.' It's what gets you ahead in life. Now, when you do this without getting punched in the chest, you'll have
more fun.
</p>
</div>
</div>
</section>

<section>
<!-- Use the 'purple' modifier class here -->
<div class="accordion purple">
<h3 class="header">
<button>Batteries and Inverters</button>
</h3>
<div class="panel">
<p>
Second set of words go here.<br/> Steve Holt! I don't criticize you! And if you're worried about criticism, sometimes a diet is the best defense. That's why you always leave a note! Well, what do you expect, mother? I don't criticize you! And
if you're worried about criticism, sometimes a diet is the best defense.<br/>
<br/> Across from where? As you may or may not know, Lindsay and I have hit a bit of a rough patch. Now, when you do this without getting punched in the chest, you'll have more fun. No… but I'd like to be asked!
</p>
</div>
</div>
</section>

<section>
<div class="accordion gray">
<h3 class="header">
<button>AC and DC Panels
</button>
</h3>
<div class="panel">
<p>
Third set of words go here.<br/> That's why you always leave a note! Not tricks, Michael, illusions. As you may or may not know, Lindsay and I have hit a bit of a rough patch. It's called 'taking advantage.' It's what gets you ahead in life.<br/>
<br/> Say goodbye to these, because it's the last time! First place chick is hot, but has an attitude, doesn't date magicians. I'm afraid I just blue myself. I'm afraid I just blue myself. I'm afraid I just blue myself.
</p>
</div>
</div>
</section>








share|improve this answer

























  • Barthy ... thank you for the code, I have tried it and it works well. The only issue I am running into is that if you have a long string of text say in the first panel and you scroll down and select the next panel you do not see the text in the newly selected panel as you are still scrolled down after the new section is made. Not to big a deal on a desk top, but bigger deal on a phone screen. Is there anything that could be added to the script to solve this?

    – Randy Morgan
    May 14 at 18:16











  • Yes, you could adjust the scrolling position of the window to show the appropriate content. Although that is an entirely different issue, which can be solved by different approaches, I will edit my answer to show you one possible way. In general, this is something you should try out by yourself and then only ask about if you find yourself stuck and have specific question to ask.

    – Barthy
    May 15 at 8:23











  • Barthy … This works exactly as I was hoping, thank you again you are a life saver. I actually did try several approaches throughout the day yesterday, but everything I came up with was very clunky and involved creating separate div names for each panel and then having to create a separate script for each panel as there will be around 10 panels it was going to get messy and code heavy in a hurry. Thank you again for your simple and elegant solution.

    – Randy Morgan
    May 15 at 15:15











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56117391%2fclose-all-open-drop-downs-and-consolidate-code-if-possible%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Question 1 — "How do I not end up with a bunch of drop downs open on the page at the same time":



You close all dropdowns before opening another one. You can also create css rules to display or hide the dropdown. This way, it will be easier to find the currently active dropdown. See code below.



Question 2 — "How can I make the list alternate colors"



You can add more than one class to an element. Simply create color classes and add them to the right elements. See code below.



Notes:



  • Use the CSS selectors instead of JavaScript to show/hide the panel


  • Element h3 is not allowed as child of element button. Do it the other way round.

  • Use the same JavaScript code and CSS for all accordions.

Edit (scrollIntoView)



I added code to automatically scroll the window so that the active tab is visible.
It works only on Chrome, Firefox and Opera. Use this polyfill iamdustan/smoothscroll to use it in other browsers. See compatibility here and all functions here.






// Query all accordions
var accordions = document.querySelectorAll('.accordion');

for (var i = 0; i < accordions.length; i++)
accordions[i].addEventListener('click', function()
// Get the currently active accordion
var active = document.querySelector('.accordion.active');

// If there is one, deactivate it
if (active)
active.classList.remove('active');


// Activate the new accordion, if it's not the one you just deactivated
if (active !== this)
this.classList.add('active');

// Use scrollIntoView to adjust the window scroll position to show the content.
this.scrollIntoView(
behavior: 'smooth'
);

);

.accordion .header button 
text-align: left;
padding: 18px;
background: transparent;
border: none;
outline: none;
cursor: pointer;
width: 100%;
color: #444;
width: 100%;
transition: 0.4s;



/* Set the color according to the modifier class */

.accordion.gray button
background-color: #eee;


.accordion.purple button
background-color: #8461E8;


.accordion.active button,
.accordion button:hover
background-color: #ccc;


.accordion .panel
padding: 0 18px;
background-color: white;
display: none;
overflow: hidden;



/* Show the panel if the accordion is active */

.accordion.active .panel
display: block;


.accordion button:after
content: '2795';
font-size: 13px;
color: #777;
float: right;
margin-left: 5px;


.accordion.active button:after
content: "2796";

<section>
<!-- Each accordion is wrapped by a div with the class 'accordion' -->
<!-- 'accordion' is the component, 'gray' is the color modifier -->
<div class="accordion gray">
<!-- h3 can contain a button -->
<h3 class="header">
<button>Alternators and regulators</button>
</h3>
<div class="panel">
<p>
First group of words go here.<br/> I'm afraid I just blue myself. No… but I'd like to be asked! Michael! It's called 'taking advantage.' It's what gets you ahead in life. Now, when you do this without getting punched in the chest, you'll have
more fun.
</p>
</div>
</div>
</section>

<section>
<!-- Use the 'purple' modifier class here -->
<div class="accordion purple">
<h3 class="header">
<button>Batteries and Inverters</button>
</h3>
<div class="panel">
<p>
Second set of words go here.<br/> Steve Holt! I don't criticize you! And if you're worried about criticism, sometimes a diet is the best defense. That's why you always leave a note! Well, what do you expect, mother? I don't criticize you! And
if you're worried about criticism, sometimes a diet is the best defense.<br/>
<br/> Across from where? As you may or may not know, Lindsay and I have hit a bit of a rough patch. Now, when you do this without getting punched in the chest, you'll have more fun. No… but I'd like to be asked!
</p>
</div>
</div>
</section>

<section>
<div class="accordion gray">
<h3 class="header">
<button>AC and DC Panels
</button>
</h3>
<div class="panel">
<p>
Third set of words go here.<br/> That's why you always leave a note! Not tricks, Michael, illusions. As you may or may not know, Lindsay and I have hit a bit of a rough patch. It's called 'taking advantage.' It's what gets you ahead in life.<br/>
<br/> Say goodbye to these, because it's the last time! First place chick is hot, but has an attitude, doesn't date magicians. I'm afraid I just blue myself. I'm afraid I just blue myself. I'm afraid I just blue myself.
</p>
</div>
</div>
</section>








share|improve this answer

























  • Barthy ... thank you for the code, I have tried it and it works well. The only issue I am running into is that if you have a long string of text say in the first panel and you scroll down and select the next panel you do not see the text in the newly selected panel as you are still scrolled down after the new section is made. Not to big a deal on a desk top, but bigger deal on a phone screen. Is there anything that could be added to the script to solve this?

    – Randy Morgan
    May 14 at 18:16











  • Yes, you could adjust the scrolling position of the window to show the appropriate content. Although that is an entirely different issue, which can be solved by different approaches, I will edit my answer to show you one possible way. In general, this is something you should try out by yourself and then only ask about if you find yourself stuck and have specific question to ask.

    – Barthy
    May 15 at 8:23











  • Barthy … This works exactly as I was hoping, thank you again you are a life saver. I actually did try several approaches throughout the day yesterday, but everything I came up with was very clunky and involved creating separate div names for each panel and then having to create a separate script for each panel as there will be around 10 panels it was going to get messy and code heavy in a hurry. Thank you again for your simple and elegant solution.

    – Randy Morgan
    May 15 at 15:15















2














Question 1 — "How do I not end up with a bunch of drop downs open on the page at the same time":



You close all dropdowns before opening another one. You can also create css rules to display or hide the dropdown. This way, it will be easier to find the currently active dropdown. See code below.



Question 2 — "How can I make the list alternate colors"



You can add more than one class to an element. Simply create color classes and add them to the right elements. See code below.



Notes:



  • Use the CSS selectors instead of JavaScript to show/hide the panel


  • Element h3 is not allowed as child of element button. Do it the other way round.

  • Use the same JavaScript code and CSS for all accordions.

Edit (scrollIntoView)



I added code to automatically scroll the window so that the active tab is visible.
It works only on Chrome, Firefox and Opera. Use this polyfill iamdustan/smoothscroll to use it in other browsers. See compatibility here and all functions here.






// Query all accordions
var accordions = document.querySelectorAll('.accordion');

for (var i = 0; i < accordions.length; i++)
accordions[i].addEventListener('click', function()
// Get the currently active accordion
var active = document.querySelector('.accordion.active');

// If there is one, deactivate it
if (active)
active.classList.remove('active');


// Activate the new accordion, if it's not the one you just deactivated
if (active !== this)
this.classList.add('active');

// Use scrollIntoView to adjust the window scroll position to show the content.
this.scrollIntoView(
behavior: 'smooth'
);

);

.accordion .header button 
text-align: left;
padding: 18px;
background: transparent;
border: none;
outline: none;
cursor: pointer;
width: 100%;
color: #444;
width: 100%;
transition: 0.4s;



/* Set the color according to the modifier class */

.accordion.gray button
background-color: #eee;


.accordion.purple button
background-color: #8461E8;


.accordion.active button,
.accordion button:hover
background-color: #ccc;


.accordion .panel
padding: 0 18px;
background-color: white;
display: none;
overflow: hidden;



/* Show the panel if the accordion is active */

.accordion.active .panel
display: block;


.accordion button:after
content: '2795';
font-size: 13px;
color: #777;
float: right;
margin-left: 5px;


.accordion.active button:after
content: "2796";

<section>
<!-- Each accordion is wrapped by a div with the class 'accordion' -->
<!-- 'accordion' is the component, 'gray' is the color modifier -->
<div class="accordion gray">
<!-- h3 can contain a button -->
<h3 class="header">
<button>Alternators and regulators</button>
</h3>
<div class="panel">
<p>
First group of words go here.<br/> I'm afraid I just blue myself. No… but I'd like to be asked! Michael! It's called 'taking advantage.' It's what gets you ahead in life. Now, when you do this without getting punched in the chest, you'll have
more fun.
</p>
</div>
</div>
</section>

<section>
<!-- Use the 'purple' modifier class here -->
<div class="accordion purple">
<h3 class="header">
<button>Batteries and Inverters</button>
</h3>
<div class="panel">
<p>
Second set of words go here.<br/> Steve Holt! I don't criticize you! And if you're worried about criticism, sometimes a diet is the best defense. That's why you always leave a note! Well, what do you expect, mother? I don't criticize you! And
if you're worried about criticism, sometimes a diet is the best defense.<br/>
<br/> Across from where? As you may or may not know, Lindsay and I have hit a bit of a rough patch. Now, when you do this without getting punched in the chest, you'll have more fun. No… but I'd like to be asked!
</p>
</div>
</div>
</section>

<section>
<div class="accordion gray">
<h3 class="header">
<button>AC and DC Panels
</button>
</h3>
<div class="panel">
<p>
Third set of words go here.<br/> That's why you always leave a note! Not tricks, Michael, illusions. As you may or may not know, Lindsay and I have hit a bit of a rough patch. It's called 'taking advantage.' It's what gets you ahead in life.<br/>
<br/> Say goodbye to these, because it's the last time! First place chick is hot, but has an attitude, doesn't date magicians. I'm afraid I just blue myself. I'm afraid I just blue myself. I'm afraid I just blue myself.
</p>
</div>
</div>
</section>








share|improve this answer

























  • Barthy ... thank you for the code, I have tried it and it works well. The only issue I am running into is that if you have a long string of text say in the first panel and you scroll down and select the next panel you do not see the text in the newly selected panel as you are still scrolled down after the new section is made. Not to big a deal on a desk top, but bigger deal on a phone screen. Is there anything that could be added to the script to solve this?

    – Randy Morgan
    May 14 at 18:16











  • Yes, you could adjust the scrolling position of the window to show the appropriate content. Although that is an entirely different issue, which can be solved by different approaches, I will edit my answer to show you one possible way. In general, this is something you should try out by yourself and then only ask about if you find yourself stuck and have specific question to ask.

    – Barthy
    May 15 at 8:23











  • Barthy … This works exactly as I was hoping, thank you again you are a life saver. I actually did try several approaches throughout the day yesterday, but everything I came up with was very clunky and involved creating separate div names for each panel and then having to create a separate script for each panel as there will be around 10 panels it was going to get messy and code heavy in a hurry. Thank you again for your simple and elegant solution.

    – Randy Morgan
    May 15 at 15:15













2












2








2







Question 1 — "How do I not end up with a bunch of drop downs open on the page at the same time":



You close all dropdowns before opening another one. You can also create css rules to display or hide the dropdown. This way, it will be easier to find the currently active dropdown. See code below.



Question 2 — "How can I make the list alternate colors"



You can add more than one class to an element. Simply create color classes and add them to the right elements. See code below.



Notes:



  • Use the CSS selectors instead of JavaScript to show/hide the panel


  • Element h3 is not allowed as child of element button. Do it the other way round.

  • Use the same JavaScript code and CSS for all accordions.

Edit (scrollIntoView)



I added code to automatically scroll the window so that the active tab is visible.
It works only on Chrome, Firefox and Opera. Use this polyfill iamdustan/smoothscroll to use it in other browsers. See compatibility here and all functions here.






// Query all accordions
var accordions = document.querySelectorAll('.accordion');

for (var i = 0; i < accordions.length; i++)
accordions[i].addEventListener('click', function()
// Get the currently active accordion
var active = document.querySelector('.accordion.active');

// If there is one, deactivate it
if (active)
active.classList.remove('active');


// Activate the new accordion, if it's not the one you just deactivated
if (active !== this)
this.classList.add('active');

// Use scrollIntoView to adjust the window scroll position to show the content.
this.scrollIntoView(
behavior: 'smooth'
);

);

.accordion .header button 
text-align: left;
padding: 18px;
background: transparent;
border: none;
outline: none;
cursor: pointer;
width: 100%;
color: #444;
width: 100%;
transition: 0.4s;



/* Set the color according to the modifier class */

.accordion.gray button
background-color: #eee;


.accordion.purple button
background-color: #8461E8;


.accordion.active button,
.accordion button:hover
background-color: #ccc;


.accordion .panel
padding: 0 18px;
background-color: white;
display: none;
overflow: hidden;



/* Show the panel if the accordion is active */

.accordion.active .panel
display: block;


.accordion button:after
content: '2795';
font-size: 13px;
color: #777;
float: right;
margin-left: 5px;


.accordion.active button:after
content: "2796";

<section>
<!-- Each accordion is wrapped by a div with the class 'accordion' -->
<!-- 'accordion' is the component, 'gray' is the color modifier -->
<div class="accordion gray">
<!-- h3 can contain a button -->
<h3 class="header">
<button>Alternators and regulators</button>
</h3>
<div class="panel">
<p>
First group of words go here.<br/> I'm afraid I just blue myself. No… but I'd like to be asked! Michael! It's called 'taking advantage.' It's what gets you ahead in life. Now, when you do this without getting punched in the chest, you'll have
more fun.
</p>
</div>
</div>
</section>

<section>
<!-- Use the 'purple' modifier class here -->
<div class="accordion purple">
<h3 class="header">
<button>Batteries and Inverters</button>
</h3>
<div class="panel">
<p>
Second set of words go here.<br/> Steve Holt! I don't criticize you! And if you're worried about criticism, sometimes a diet is the best defense. That's why you always leave a note! Well, what do you expect, mother? I don't criticize you! And
if you're worried about criticism, sometimes a diet is the best defense.<br/>
<br/> Across from where? As you may or may not know, Lindsay and I have hit a bit of a rough patch. Now, when you do this without getting punched in the chest, you'll have more fun. No… but I'd like to be asked!
</p>
</div>
</div>
</section>

<section>
<div class="accordion gray">
<h3 class="header">
<button>AC and DC Panels
</button>
</h3>
<div class="panel">
<p>
Third set of words go here.<br/> That's why you always leave a note! Not tricks, Michael, illusions. As you may or may not know, Lindsay and I have hit a bit of a rough patch. It's called 'taking advantage.' It's what gets you ahead in life.<br/>
<br/> Say goodbye to these, because it's the last time! First place chick is hot, but has an attitude, doesn't date magicians. I'm afraid I just blue myself. I'm afraid I just blue myself. I'm afraid I just blue myself.
</p>
</div>
</div>
</section>








share|improve this answer















Question 1 — "How do I not end up with a bunch of drop downs open on the page at the same time":



You close all dropdowns before opening another one. You can also create css rules to display or hide the dropdown. This way, it will be easier to find the currently active dropdown. See code below.



Question 2 — "How can I make the list alternate colors"



You can add more than one class to an element. Simply create color classes and add them to the right elements. See code below.



Notes:



  • Use the CSS selectors instead of JavaScript to show/hide the panel


  • Element h3 is not allowed as child of element button. Do it the other way round.

  • Use the same JavaScript code and CSS for all accordions.

Edit (scrollIntoView)



I added code to automatically scroll the window so that the active tab is visible.
It works only on Chrome, Firefox and Opera. Use this polyfill iamdustan/smoothscroll to use it in other browsers. See compatibility here and all functions here.






// Query all accordions
var accordions = document.querySelectorAll('.accordion');

for (var i = 0; i < accordions.length; i++)
accordions[i].addEventListener('click', function()
// Get the currently active accordion
var active = document.querySelector('.accordion.active');

// If there is one, deactivate it
if (active)
active.classList.remove('active');


// Activate the new accordion, if it's not the one you just deactivated
if (active !== this)
this.classList.add('active');

// Use scrollIntoView to adjust the window scroll position to show the content.
this.scrollIntoView(
behavior: 'smooth'
);

);

.accordion .header button 
text-align: left;
padding: 18px;
background: transparent;
border: none;
outline: none;
cursor: pointer;
width: 100%;
color: #444;
width: 100%;
transition: 0.4s;



/* Set the color according to the modifier class */

.accordion.gray button
background-color: #eee;


.accordion.purple button
background-color: #8461E8;


.accordion.active button,
.accordion button:hover
background-color: #ccc;


.accordion .panel
padding: 0 18px;
background-color: white;
display: none;
overflow: hidden;



/* Show the panel if the accordion is active */

.accordion.active .panel
display: block;


.accordion button:after
content: '2795';
font-size: 13px;
color: #777;
float: right;
margin-left: 5px;


.accordion.active button:after
content: "2796";

<section>
<!-- Each accordion is wrapped by a div with the class 'accordion' -->
<!-- 'accordion' is the component, 'gray' is the color modifier -->
<div class="accordion gray">
<!-- h3 can contain a button -->
<h3 class="header">
<button>Alternators and regulators</button>
</h3>
<div class="panel">
<p>
First group of words go here.<br/> I'm afraid I just blue myself. No… but I'd like to be asked! Michael! It's called 'taking advantage.' It's what gets you ahead in life. Now, when you do this without getting punched in the chest, you'll have
more fun.
</p>
</div>
</div>
</section>

<section>
<!-- Use the 'purple' modifier class here -->
<div class="accordion purple">
<h3 class="header">
<button>Batteries and Inverters</button>
</h3>
<div class="panel">
<p>
Second set of words go here.<br/> Steve Holt! I don't criticize you! And if you're worried about criticism, sometimes a diet is the best defense. That's why you always leave a note! Well, what do you expect, mother? I don't criticize you! And
if you're worried about criticism, sometimes a diet is the best defense.<br/>
<br/> Across from where? As you may or may not know, Lindsay and I have hit a bit of a rough patch. Now, when you do this without getting punched in the chest, you'll have more fun. No… but I'd like to be asked!
</p>
</div>
</div>
</section>

<section>
<div class="accordion gray">
<h3 class="header">
<button>AC and DC Panels
</button>
</h3>
<div class="panel">
<p>
Third set of words go here.<br/> That's why you always leave a note! Not tricks, Michael, illusions. As you may or may not know, Lindsay and I have hit a bit of a rough patch. It's called 'taking advantage.' It's what gets you ahead in life.<br/>
<br/> Say goodbye to these, because it's the last time! First place chick is hot, but has an attitude, doesn't date magicians. I'm afraid I just blue myself. I'm afraid I just blue myself. I'm afraid I just blue myself.
</p>
</div>
</div>
</section>








// Query all accordions
var accordions = document.querySelectorAll('.accordion');

for (var i = 0; i < accordions.length; i++)
accordions[i].addEventListener('click', function()
// Get the currently active accordion
var active = document.querySelector('.accordion.active');

// If there is one, deactivate it
if (active)
active.classList.remove('active');


// Activate the new accordion, if it's not the one you just deactivated
if (active !== this)
this.classList.add('active');

// Use scrollIntoView to adjust the window scroll position to show the content.
this.scrollIntoView(
behavior: 'smooth'
);

);

.accordion .header button 
text-align: left;
padding: 18px;
background: transparent;
border: none;
outline: none;
cursor: pointer;
width: 100%;
color: #444;
width: 100%;
transition: 0.4s;



/* Set the color according to the modifier class */

.accordion.gray button
background-color: #eee;


.accordion.purple button
background-color: #8461E8;


.accordion.active button,
.accordion button:hover
background-color: #ccc;


.accordion .panel
padding: 0 18px;
background-color: white;
display: none;
overflow: hidden;



/* Show the panel if the accordion is active */

.accordion.active .panel
display: block;


.accordion button:after
content: '2795';
font-size: 13px;
color: #777;
float: right;
margin-left: 5px;


.accordion.active button:after
content: "2796";

<section>
<!-- Each accordion is wrapped by a div with the class 'accordion' -->
<!-- 'accordion' is the component, 'gray' is the color modifier -->
<div class="accordion gray">
<!-- h3 can contain a button -->
<h3 class="header">
<button>Alternators and regulators</button>
</h3>
<div class="panel">
<p>
First group of words go here.<br/> I'm afraid I just blue myself. No… but I'd like to be asked! Michael! It's called 'taking advantage.' It's what gets you ahead in life. Now, when you do this without getting punched in the chest, you'll have
more fun.
</p>
</div>
</div>
</section>

<section>
<!-- Use the 'purple' modifier class here -->
<div class="accordion purple">
<h3 class="header">
<button>Batteries and Inverters</button>
</h3>
<div class="panel">
<p>
Second set of words go here.<br/> Steve Holt! I don't criticize you! And if you're worried about criticism, sometimes a diet is the best defense. That's why you always leave a note! Well, what do you expect, mother? I don't criticize you! And
if you're worried about criticism, sometimes a diet is the best defense.<br/>
<br/> Across from where? As you may or may not know, Lindsay and I have hit a bit of a rough patch. Now, when you do this without getting punched in the chest, you'll have more fun. No… but I'd like to be asked!
</p>
</div>
</div>
</section>

<section>
<div class="accordion gray">
<h3 class="header">
<button>AC and DC Panels
</button>
</h3>
<div class="panel">
<p>
Third set of words go here.<br/> That's why you always leave a note! Not tricks, Michael, illusions. As you may or may not know, Lindsay and I have hit a bit of a rough patch. It's called 'taking advantage.' It's what gets you ahead in life.<br/>
<br/> Say goodbye to these, because it's the last time! First place chick is hot, but has an attitude, doesn't date magicians. I'm afraid I just blue myself. I'm afraid I just blue myself. I'm afraid I just blue myself.
</p>
</div>
</div>
</section>





// Query all accordions
var accordions = document.querySelectorAll('.accordion');

for (var i = 0; i < accordions.length; i++)
accordions[i].addEventListener('click', function()
// Get the currently active accordion
var active = document.querySelector('.accordion.active');

// If there is one, deactivate it
if (active)
active.classList.remove('active');


// Activate the new accordion, if it's not the one you just deactivated
if (active !== this)
this.classList.add('active');

// Use scrollIntoView to adjust the window scroll position to show the content.
this.scrollIntoView(
behavior: 'smooth'
);

);

.accordion .header button 
text-align: left;
padding: 18px;
background: transparent;
border: none;
outline: none;
cursor: pointer;
width: 100%;
color: #444;
width: 100%;
transition: 0.4s;



/* Set the color according to the modifier class */

.accordion.gray button
background-color: #eee;


.accordion.purple button
background-color: #8461E8;


.accordion.active button,
.accordion button:hover
background-color: #ccc;


.accordion .panel
padding: 0 18px;
background-color: white;
display: none;
overflow: hidden;



/* Show the panel if the accordion is active */

.accordion.active .panel
display: block;


.accordion button:after
content: '2795';
font-size: 13px;
color: #777;
float: right;
margin-left: 5px;


.accordion.active button:after
content: "2796";

<section>
<!-- Each accordion is wrapped by a div with the class 'accordion' -->
<!-- 'accordion' is the component, 'gray' is the color modifier -->
<div class="accordion gray">
<!-- h3 can contain a button -->
<h3 class="header">
<button>Alternators and regulators</button>
</h3>
<div class="panel">
<p>
First group of words go here.<br/> I'm afraid I just blue myself. No… but I'd like to be asked! Michael! It's called 'taking advantage.' It's what gets you ahead in life. Now, when you do this without getting punched in the chest, you'll have
more fun.
</p>
</div>
</div>
</section>

<section>
<!-- Use the 'purple' modifier class here -->
<div class="accordion purple">
<h3 class="header">
<button>Batteries and Inverters</button>
</h3>
<div class="panel">
<p>
Second set of words go here.<br/> Steve Holt! I don't criticize you! And if you're worried about criticism, sometimes a diet is the best defense. That's why you always leave a note! Well, what do you expect, mother? I don't criticize you! And
if you're worried about criticism, sometimes a diet is the best defense.<br/>
<br/> Across from where? As you may or may not know, Lindsay and I have hit a bit of a rough patch. Now, when you do this without getting punched in the chest, you'll have more fun. No… but I'd like to be asked!
</p>
</div>
</div>
</section>

<section>
<div class="accordion gray">
<h3 class="header">
<button>AC and DC Panels
</button>
</h3>
<div class="panel">
<p>
Third set of words go here.<br/> That's why you always leave a note! Not tricks, Michael, illusions. As you may or may not know, Lindsay and I have hit a bit of a rough patch. It's called 'taking advantage.' It's what gets you ahead in life.<br/>
<br/> Say goodbye to these, because it's the last time! First place chick is hot, but has an attitude, doesn't date magicians. I'm afraid I just blue myself. I'm afraid I just blue myself. I'm afraid I just blue myself.
</p>
</div>
</div>
</section>






share|improve this answer














share|improve this answer



share|improve this answer








edited May 15 at 8:33

























answered May 13 at 23:03









BarthyBarthy

1,5111131




1,5111131












  • Barthy ... thank you for the code, I have tried it and it works well. The only issue I am running into is that if you have a long string of text say in the first panel and you scroll down and select the next panel you do not see the text in the newly selected panel as you are still scrolled down after the new section is made. Not to big a deal on a desk top, but bigger deal on a phone screen. Is there anything that could be added to the script to solve this?

    – Randy Morgan
    May 14 at 18:16











  • Yes, you could adjust the scrolling position of the window to show the appropriate content. Although that is an entirely different issue, which can be solved by different approaches, I will edit my answer to show you one possible way. In general, this is something you should try out by yourself and then only ask about if you find yourself stuck and have specific question to ask.

    – Barthy
    May 15 at 8:23











  • Barthy … This works exactly as I was hoping, thank you again you are a life saver. I actually did try several approaches throughout the day yesterday, but everything I came up with was very clunky and involved creating separate div names for each panel and then having to create a separate script for each panel as there will be around 10 panels it was going to get messy and code heavy in a hurry. Thank you again for your simple and elegant solution.

    – Randy Morgan
    May 15 at 15:15

















  • Barthy ... thank you for the code, I have tried it and it works well. The only issue I am running into is that if you have a long string of text say in the first panel and you scroll down and select the next panel you do not see the text in the newly selected panel as you are still scrolled down after the new section is made. Not to big a deal on a desk top, but bigger deal on a phone screen. Is there anything that could be added to the script to solve this?

    – Randy Morgan
    May 14 at 18:16











  • Yes, you could adjust the scrolling position of the window to show the appropriate content. Although that is an entirely different issue, which can be solved by different approaches, I will edit my answer to show you one possible way. In general, this is something you should try out by yourself and then only ask about if you find yourself stuck and have specific question to ask.

    – Barthy
    May 15 at 8:23











  • Barthy … This works exactly as I was hoping, thank you again you are a life saver. I actually did try several approaches throughout the day yesterday, but everything I came up with was very clunky and involved creating separate div names for each panel and then having to create a separate script for each panel as there will be around 10 panels it was going to get messy and code heavy in a hurry. Thank you again for your simple and elegant solution.

    – Randy Morgan
    May 15 at 15:15
















Barthy ... thank you for the code, I have tried it and it works well. The only issue I am running into is that if you have a long string of text say in the first panel and you scroll down and select the next panel you do not see the text in the newly selected panel as you are still scrolled down after the new section is made. Not to big a deal on a desk top, but bigger deal on a phone screen. Is there anything that could be added to the script to solve this?

– Randy Morgan
May 14 at 18:16





Barthy ... thank you for the code, I have tried it and it works well. The only issue I am running into is that if you have a long string of text say in the first panel and you scroll down and select the next panel you do not see the text in the newly selected panel as you are still scrolled down after the new section is made. Not to big a deal on a desk top, but bigger deal on a phone screen. Is there anything that could be added to the script to solve this?

– Randy Morgan
May 14 at 18:16













Yes, you could adjust the scrolling position of the window to show the appropriate content. Although that is an entirely different issue, which can be solved by different approaches, I will edit my answer to show you one possible way. In general, this is something you should try out by yourself and then only ask about if you find yourself stuck and have specific question to ask.

– Barthy
May 15 at 8:23





Yes, you could adjust the scrolling position of the window to show the appropriate content. Although that is an entirely different issue, which can be solved by different approaches, I will edit my answer to show you one possible way. In general, this is something you should try out by yourself and then only ask about if you find yourself stuck and have specific question to ask.

– Barthy
May 15 at 8:23













Barthy … This works exactly as I was hoping, thank you again you are a life saver. I actually did try several approaches throughout the day yesterday, but everything I came up with was very clunky and involved creating separate div names for each panel and then having to create a separate script for each panel as there will be around 10 panels it was going to get messy and code heavy in a hurry. Thank you again for your simple and elegant solution.

– Randy Morgan
May 15 at 15:15





Barthy … This works exactly as I was hoping, thank you again you are a life saver. I actually did try several approaches throughout the day yesterday, but everything I came up with was very clunky and involved creating separate div names for each panel and then having to create a separate script for each panel as there will be around 10 panels it was going to get messy and code heavy in a hurry. Thank you again for your simple and elegant solution.

– Randy Morgan
May 15 at 15:15



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56117391%2fclose-all-open-drop-downs-and-consolidate-code-if-possible%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to write a 12-bar blues melodyI-IV-V blues progressionHow to play the bridges in a standard blues progressionHow does Gdim7 fit in C# minor?question on a certain chord progressionMusicology of Melody12 bar blues, spread rhythm: alternative to 6th chord to avoid finger stretchChord progressions/ Root key/ MelodiesHow to put chords (POP-EDM) under a given lead vocal melody (starting from a good knowledge in music theory)Are there “rules” for improvising with the minor pentatonic scale over 12-bar shuffle?Confusion about blues scale and chords

What if the end-user didn't have the required library?What is setup.py?What is a clean, pythonic way to have multiple constructors in Python?What does Ruby have that Python doesn't, and vice versa?What is the reason for having '//' in Python?How do I create a namespace package in Python?How to package shared objects that python modules depend on?setuptools vs. distutils: why is distutils still a thing?Navigation in Windows 10 vs code not going to virtualenv library when the same library is installed at user levelPython create package for local usePackaging a project that uses multiple python versionsWhy is permission denied on pip install except for when “--user” is included at end of command?

Why did Thanos need his ship to help him in the battle scene?Which actor plays Thanos in the Avengers mid-credits scene?Are there economic implications portrayed in comics where the buildings and cities are ruined almost daily?Old X-Men comic where team travels to alien world with a ring-like sun that needs recharging?Why does Ego need help sleeping?Is there an objective answer to who “the strongest Avenger” is?How did Banner get unstuck?Why did Thanos get hit?How did Thanos (or anyone) know the Infinity Stones would give him this power?Did Thanos leave Eitri alive for his after-sales service?In Avengers 1, why does Thanos need Loki?