Stacksof99
Stacksof99

Reputation: 97

How to toggle style in JavaScript

I'd like to toggle stylein JavaScript. Not familiar with JavaScript but could I somehow toggle this:

document.body.style.overflow = 'hidden' 

It's part of this function:

<a id="mobile-nav" href="#">
  <div class="container1" onclick="myFunction(this)">
    <div class="bar1"></div>
    <div class="bar2"></div>
    <div class="bar3"></div>
  </div>
</a>  
function myFunction(x) {
  x.classList.toggle('change');
  document.body.style.overflow = 'hidden' 
} 

I'd like it to apply when I press it, and not apply when I press it again. At the moment it applies when i initiate the function, but doesn't stop applying when I retoggle the function.

Upvotes: 1

Views: 479

Answers (1)

DMITRYTISHKIN
DMITRYTISHKIN

Reputation: 526

document.body.style.overflow = document.body.style.overflow ? null : 'hidden';

Upvotes: 2

Related Questions