Toggle script problems how can I fix this in pure JavaScript? No jQuery

user9258655

I have made this script and I design it in a way when you click any where outside of the div the div should disappear but I notice that when you click on the div it self it disappear as well. How can I prevent that from occurring I only want it to work by clicking any where out side the div to make the div disappear not by clicking on the div to make it disappear as well. So i'm trying to prevent that. How can I do that? And I still want the toggle affect on the button as well.

document.addEventListener('DOMContentLoaded', function() {

  var bx = document.getElementsByTagName('button')[0];
  bx.addEventListener('click', fx);

  function fx() {
    document.getElementById("myDropdown").classList.toggle("show");
  }

  window.onclick = function(event) {
    if (!event.target.matches('.dropbtn')) {

      var dropdowns = document.getElementsByClassName("dropdown-content");
      var i;
      for (i = 0; i < dropdowns.length; i++) {
        var openDropdown = dropdowns[i];
        if (openDropdown.classList.contains('show')) {
          openDropdown.classList.remove('show');
        }
      }
    }
  }
});
.dropdown-content {
  display: none;
}

.show {
  display: block;
}
<button class="dropbtn">Dropdown</button>

<div id="myDropdown" class="dropdown-content">
  <h1>hello</h1>
  <img src='http://full.creative.touchtalent.com/The-enchanted-forest-97329.jpg'>
</div>

AuxTaco

Your window.onclick handler only checks to see if you clicked the button, not inside the <div>. Include the <div> check in your condition:

if (!event.target.matches('.dropbtn') && !event.target.closest('.dropdown-content')) {

Element.closest returns null if the element does not match and is not a descendant of an element that matches its argument.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I convert jQuery's $.get to pure JavaScript?

How can I properly convert the following Jquery to pure Javascript?

How can I convert this function from jquery to pure javascript?

How can I toggle the color of the background without Javascript or Jquery?

How to toggle two span texts with pure Javascript, No jQuery please

jquery toggle slide animation to pure javascript animation

How do I fix jQuery's toggle function when not toggling?

How do I fix charset problems in .gs script?

How can I use the object's functions in pure JavaScript, without using jquery?

How can I retrieve text, line by line, from 'contenteditable' to variables in pure javascript, no jquery

How can I fix my Bash script?

How can i fix thix batch script

Can I put jquery code in a bunch of pure javascript code?

How can I fix this pixelate bugs with canvas NodeJS problems?

How can I fix mobile problems placing divs side by side?

How can I fix problems while running .exe file

How can I fix my problems with fonts in Firefox?

How can I fix the XSS with Django + jQuery?

How can I fix this jquery issue?

How can I fix the warping issue in jQuery?

How to toggle in Javascript / jQuery

How can I translate jQuery ':not(:has(*)):not(a *):not(a)' to pure js for the TreeWalker?

toggle display in pure javascript

Toggle image in pure javascript

How can I fix blocking JavaScript processes?

How do I check if file exists in jQuery or pure JavaScript?

How can I check that an element is visible with Puppeteer and pure JavaScript?

How can I add and remove an active class to an element in pure JavaScript

How can I access GPT data with pure javascript on browser console?