skip to Main Content

Below is JS Code

document.addEventListener("DOMContentLoaded,");

const toggleBtn = document.getElementById('toggle_btn');
const dropDownMenu = document.getElementById('dropdown_menu');

toggleBtn.onclick = function(){
    dropDownMenu.classList.toggle('open');
}

I want the dropdown menu to get toggled when clicked in the responsive design

2

Answers


  1. It seems like you are trying to toggle the visibility of a dropdown menu when a button is clicked. The code you provided looks correct but i rewrote it so it should work:

    toggleBtn.onclick = function(){
    if (dropDownMenu.style.display === "none") {
        dropDownMenu.style.display = "block";
    } else {
        dropDownMenu.style.display = "none";
    }}
    

    Or you can use jQuery to toggle the visibility of the element with the "toggle" method:

    $('#toggle_btn').click(function(){
    $('#dropdown_menu').toggle();});
    
    Login or Signup to reply.
  2. try it….. 😊

    function toggleClass(){
      const toggleBtn = document.getElementById('toggle_btn');
      const dropDownMenu = document.getElementById('dropdown_menu');
    
      toggleBtn.addEventListener("click", function () {
        dropDownMenu.classList.toggle("open");
      });
    }
     
    document.addEventListener("DOMContentLoaded",toggleClass);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search