javascripthtmlcssdomcontentloaded

How to make menu close before redirecting to website section


I have a JQuery working code that is closing the navigation before redirecting to the right section. I want to change it for pure javascript. Do you have any ideas how to make it happen? I've searched many topics but didn't find it.

$(document).ready(function() {
$('.menu-mobile a').click(function(e) {
 e.preventDefault(); 
 closeNav();

  setTimeout(() => {
     const nextPage = e.currentTarget.href;
    window.location.href = nextPage;
  },1000) // set the time here in milliseconds    
})
});

Additionally, I would like to change $(document).ready to javascript. Is it valid replacer?

document.addEventListener("DOMContentLoaded", function() { 
  //code
});

Solution

  • For example, something like following, you just need closeNav(); function.

    document.querySelector(".link").addEventListener("click", function(e)  { 
      e.preventDefault();
      console.log(e.target.href)
      //closeNav();
      setTimeout(() => {
        const nextPage = e.target.href;
        window.location.href = nextPage;
      }, 2000)  
    });
    <div class=".menu-mobile ">
      <a class="link" href="https://www.google.com/">link</a>
    </div>