0

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.

Wallami
  • 1
  • 2
  • `y.style.display,p.style.display = "hidden";` is equivalent to just `p.style.display = "hidden";` - the first part has no effect, since you're using the [comma operator](https://stackoverflow.com/questions/3561043/what-does-a-comma-do-in-javascript-expressions) – VLAZ Sep 29 '19 at 14:59
  • Just FYI, "Java" is not a correct abbreviation for JavaScript. Java is a completely different language. – gilbert-v Sep 29 '19 at 19:35
  • you're absolutely right, was a slip on my part. sorry about that. ~ has been updated – Wallami Sep 29 '19 at 20:18

0 Answers0