Jump to content
8OO

Accordion Issue

Recommended Posts

You can put that tag in the head to load that file, and the code in that file should use the DOMContentLoaded event to make sure the code runs after the page finishes loading.

  • Thanks 1

Share this post


Link to post
Share on other sites

Oh I see...

So you're saying Accordion.js should be:

document.addEventListener("DOMContentLoaded", function(){
var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
  acc[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var panel = this.nextElementSibling;
    if (panel.style.display === "block") {
      panel.style.display = "none";
    } else {
      panel.style.display = "block";
    }
  });
}

 

And then the head, should be this:

<script type = "text/javascript" src="Accordion.js" ></script>

Right? I apologize for repeatedly asking if it's written correctly, I just want to make sure I'm not missing what you and @dsonesuk are saying

Share this post


Link to post
Share on other sites

AT LONG LAST, IT FINALLY WORKS!!!! @dsonesuk & @justsomeguy, THANK YOU BOTH SO MUCH FOR ALL YOUR HELP!! I finally got the code to work for my CMS!

Putting this down for anyone struggling with accordions on their CMS:

Accordion.js (External JavaScript File):

document.addEventListener("DOMContentLoaded", function(){
var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
  acc[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var panel = this.nextElementSibling;
    if (panel.style.display === "block") {
      panel.style.display = "none";
    } else {
      panel.style.display = "block";
    }
  });
}
});

 

CSS & JavaScript in <head>:

<style>
.accordion {
  background-color: #eee;
  color: #444;
  cursor: pointer;
  padding: 18px;
  width: 100%;
  border: none;
  text-align: left;
  outline: none;
  font-size: 15px;
  transition: 0.4s;
}

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

.panel {
  padding: 0 18px;
  display: none;
  background-color: white;
  overflow: hidden;
}
</style>

<script src="Accordion.js"</script>

HTML

<h2>Accordion</h2>
	<div>
		<button class="accordion">Section 1</button>
		<div class="panel">
			<p>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.</p>
		</div>

		<button class="accordion">Section 2</button>
		<div class="panel">
			<p>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.</p>
		</div>

		<button class="accordion">Section 3</button>
		<div class="panel">
			<p>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.</p>
		</div>
	</div>

 

Share this post


Link to post
Share on other sites

So I'm assuming that for my CMS anytime I need to use JavaScript in my code for some of the stuff shown in W3, I would need to use this in order for it to run?

document.addEventListener("DOMContentLoaded", function(){

Share this post


Link to post
Share on other sites

If the Javascript code is in the head, and it needs to interact with elements on the page, yes you need to run it after the page loads.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...