New to JavaScript here. When i click on the accordion menu, i wanted the div to expand down to show x, and if y or p is open, i want it to close those expanded menu commands.
the error is with the if statements below.
this is my attempt at the code:
function openPage() {
var x = document.getElementById("accordian.Home"),
var y = document.getElementById("accordian.Contact"),
var p = document.getElementById("accordian.Pricing");
if (x.style.display === "hidden") {
x.style.display = "block";
} else {
y.style.display,p.style.display = "hidden";
}
if (y.style.display === "hidden") {
y.style.display = "block";
} else {
x.style.display,p.style.display = "hidden";
}
if (p.style.display === "hidden") {
p.style.display = "block";
} else {
x.style.display,y.style.display = "hidden";
}
}
original script to open the accordion:
<script>
var acc = document.getElementsByClassName("accordion");
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("active");
/* 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";
}
});
}
</script>
js file to tab through the content page open:
function openPage(evt, pageName) {
var i, tabcontent, accordion;
// Get all elements with class="tabcontent" and hide them
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
// Get all elements with class="tablinks" and remove the class "active"
accordion = document.getElementsByClassName("accordion");
for (i = 0; i < accordion.length; i++) {
accordion[i].className = accordion[i].className.replace(" active", "");
}
// Show the current tab, and add an "active" class to the link that opened the tab
document.getElementById(pageName).style.display = "block";
evt.currentTarget.className += " active";
}
// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
thanks in advance! very sorry if this is an easy solution, or if it has been asked a million times.