atheneor Posted January 15, 2020 Share Posted January 15, 2020 Hi guys, I tried to convert the following jquery code to vanilla js. It seems like I've made mistake. $(".menu-link").click(function() { $("#menu").toggleClass("active"); $(".container").toggleClass("active"); }); document.querySelectorAll(".menu-link").addEventListener("click", () => { var menu = document.querySelector("#menu"); menu.classList.toggle("active"); var container = document.querySelector(".container"); container.classList.toggle("active"); }); Thanks a lot! Link to comment Share on other sites More sharing options...
gebeer Posted January 16, 2020 Share Posted January 16, 2020 Hello, and welcome to the forum. This is not really a ProcessWire specific question. You might get advice or even find a solution on stackoverflow.com. And http://youmightnotneedjquery.com/ also might have some useful info. Link to comment Share on other sites More sharing options...
BitPoet Posted January 16, 2020 Share Posted January 16, 2020 9 hours ago, atheneor said: It seems like I've made mistake. Just one, I think. document.querySelectorAll returns a NodeList. Unlike with jquery collections, you can't call addEventListener on the NodeList but rather need to iterate over its elements and attach a listener to each one. Array.forEach.call(nodeList, function(el) { el.addEventListener(/* your class toggle code here */); }) 2 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now