8OO Posted August 19, 2019 Author Share Posted August 19, 2019 I figured as much. So would this work then: <script type = "text/javascript" src="Accordion.js" ></script> Link to comment Share on other sites More sharing options...
justsomeguy Posted August 19, 2019 Share Posted August 19, 2019 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. 1 Link to comment Share on other sites More sharing options...
8OO Posted August 19, 2019 Author Share Posted August 19, 2019 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 Link to comment Share on other sites More sharing options...
dsonesuk Posted August 19, 2019 Share Posted August 19, 2019 Type attribute is no longer required. 1 Link to comment Share on other sites More sharing options...
8OO Posted August 19, 2019 Author Share Posted August 19, 2019 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> Link to comment Share on other sites More sharing options...
8OO Posted August 19, 2019 Author Share Posted August 19, 2019 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(){ Link to comment Share on other sites More sharing options...
justsomeguy Posted August 20, 2019 Share Posted August 20, 2019 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. Link to comment Share on other sites More sharing options...
8OO Posted August 20, 2019 Author Share Posted August 20, 2019 Got it! I'll make sure to do that then. Hopefully I won't have any more issues with any more things I would like to take from W3. Once again, thanks @justsomeguy and @dsonesuk, your help on this has been truly appreciated! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now