make javascript listen to an html element and manipulate the value of the property of another element if there is click

Asked

Viewed 106 times

0

I need to listen to an html element of id="hamburger", if the user clicks, javascript checks the value of the display property of another element, in the case id="Overlaynav", and changes the value, case display="None" turns into "block" and vice versa.

is giving syntax error, someone can exemplify how I make it happen?

I would like to make the whole box of my Overlaynav click sensitive, in case the user click on any screen area where there is no one <li>, the Overlaynav display becomes "None" tbm

<!-- Script do OverlayNav -->    

<script>
    document.getElementById("hamburger").addEventListener("click", function() {
        var status = OverlayNav.style.display = '';
        if (status === 'none') {
            var status = OverlayNav.style.display = 'block';
        } else {
            var status = OverlayNav.style.display = 'none';
        }
    });
</script>

1 answer

1


If code had some errors, the first is that you did not declare the pro variable OverlayNav after you put the variable status = OverlayNav.style.display = '' as a value = '' and you can just put the variable as OverlayNav.style.display and leaves to set the none or block in condition if/else

inserir a descrição da imagem aqui

And if you want to click on OverlayNav He shuts you put another Listener with a function to click on it in case it is clicked on display:none

var OverlayNav = document.getElementById("OverlayNav");

OverlayNav.addEventListener('click', function() {
  this.style.display = 'none';
})


document.getElementById("hamburger").addEventListener("click", function() {

  var status = OverlayNav.style.display;

  if (status == 'none') {
      OverlayNav.style.display = 'block';
  } else {
      OverlayNav.style.display = 'none';
  }

});
<div id="hamburger">hamburger</div>

<div id="OverlayNav">OverlayNav</div>

  • like you said, you wouldn’t even need to create a new var status where is changing the display of the element, would be even better :)

  • @Ricardopontual vdd, in fact, is that as I started from his code I didn’t even tend to the details

  • I get it. I actually think it’s really cool that animated gifs

  • @Ricardopunctual is a 3MB program called Screentogif, super light and easy to use, recommend

  • 1

    good recommendation, downloading here :)

  • cool, it worked =D another detail, there is a script that changes the class of the hamburger button when clicked, this.classList.toggle("is-active"); when you click on the overlay box, it disappears but the button continues with the class ("is-active") how do I return the class of the button if the overlay is display:None ?

  • @Nicholasaffonso in the function you click on the overlay vc puts in to remove the is-active class from the hamburger, would look like this for example document.getElementById("hamburger").classList.remove('is-active');, you have to run this when you click on the overlay

  • 1

    perfect, everything working accordingly

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.