Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

46
Views
How do I toggle aria-expanded whilst also changing content and hiding element?

Using vanilla JS, I'm trying to toggle the visibility of an element and then change the text of the button whilst also changing the aria-expanded.

If you click on the button, then the list should show as-well as the button changing text, and if you click on it again, then the list will be hidden as-well as the button text changing.

Below is the code but can't seem to figure out why it's not working?

  var featureListToggleButton = document.getElementById(
    "FeatureList-toggle-button"
  );
  var featureList = document.getElementById("FeaturesList");

  featureListToggleButton.addEventListener("click", function () {
    featureListToggleButton.getAttribute(
      "aria-expanded",
      function (index, value) {
        var isExpanded = value === "true";
        featureList.classList.toggle("hide");
        featureListToggleButton.textContent(isExpanded ? "Show" : "Hide");

        return !isExpanded;
      }
    );
  });
7 months ago ยท Juan Pablo Isaza
Answer question
Find remote jobs