Hi I'm new at Javascript but am trying to use it to create an accordion in Wordpress. I'm using this exact code from W3:
http://www.w3schools.com/howto/howto_js_accordion.asp
The CSS works because I have an overwrite file plugin, the HTML I can do in the text tab of the page in wordpress. But the js isn't working. Currently the way it is set up is this:
Update: Here is the code in the wordpress page box:
<button class="accordion">Section 1</button>
<div class="panel">
<p>Lorem ipsum...</p>
</div>
Here is the code in my CSS file:
button.accordion {
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
text-align: left;
border: none;
outline: none;
transition: 0.4s;
}
button.accordion.active, button.accordion:hover {
background-color: #ddd;
}
div.panel {
padding: 0 18px;
background-color: white;
display: none;
}
div.panel.show {
display: block;
}
Here is the JS in my external file:
function js_in_faq() {
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].onclick = function(){
this.classList.toggle("active");
this.nextElementSibling.classList.toggle("show");
}
}
}
I'm calling it by adding this in the header.php:
<script type="text/javascript" src="/js/bigjava.js"></script>
And then I added the JS to the wordpress page:
<script type="text/javascript">
js_in_faq();
</script>