[javascript] How do I toggle an element's class in pure JavaScript?

I'm looking for a way to convert this jQuery code (which is used in responsive menu section) to pure JavaScript.

If it's hard to implement it's OK to use other JavaScript frameworks.


    if (typeof masonryGallery != 'undefined') 

This question is related to javascript dom toggle

The answer is

2014 answer: classList.toggle() is the standard and supported by most browsers.

Older browsers can use use classlist.js for classList.toggle():

var menu = document.querySelector('.menu') // Using a class instead, see note below.

As an aside, you shouldn't be using IDs (they leak globals into the JS window object).

Similar questions with javascript tag:

Similar questions with dom tag:

Similar questions with toggle tag: