Home Is there an easier way to repeat multiple lines of code JQUERY
Reply: 0

Is there an easier way to repeat multiple lines of code JQUERY

user1261
1#
user1261 Published in July 23, 2018, 1:37 pm

I have multiple functions that use different elements to perform the same task. This is for a mobile menu. So I have a function that when menu button is clicked..open mobile menu, transform hamburger to an "X" and make the page overlay visible. I also have a function that closes the menu when the menu is open and the user changes the orientation of the device. I also have a function when the user clicks/taps anywhere outside of the mobile menu it will close the menu (as well as clicking "X").

In these functions I repeat myself a lot, using "removeClass or toggleClass". Is there an easier way to represent all of these lines of code so that they're declared once somewhere else and then called within all of these functions? A variable maybe? Because I don't like repeating myself.

This is the repeat/example code.

$("#mobile-menu").removeClass("enabled1");
$("#menu-button").removeClass("open");
$("#mobile-menu-wrapper").removeClass("enabled1");
$("#page-overlay").removeClass("enabled1");

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.447482 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO