A
A
Andrey Borchik2018-02-24 08:26:59
JavaScript
Andrey Borchik, 2018-02-24 08:26:59

Why does the above code not work correctly in SAFARI and FIREFOX?

function myFunction() {
  var x = document.getElementById("myDIV");
  if (x.style.display === "none") {
    function closeModalOnClickOutside(){
      if (!event.target.closest('#myDIV') && !event.target.closest('#myLink') && document.getElementById("myDIV").style.display !== "none") {
        console.log('close');
        window.removeEventListener('mousedown', closeModalOnClickOutside);
        document.getElementById("myDIV").style.display = "none";
      }
    }
    window.addEventListener('mousedown', closeModalOnClickOutside);
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}

This code handles the display of the menu when an element is clicked.
When you click again, the menu disappears.
Also disappears when you click in the background side.
In Safari and Firefox, hiding the menu does not work when clicking in the background. Works correctly in Chrome.
For example - the site Arthata.by - click on the Price element at the top of the screen.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kreotech, 2018-02-24
@andryu6ka

And nothing that you use event is not clear where it came from in the closeModalOnClickOutside
function It must be added to the function parameters

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question