JavaScript Hiding div with Javascript hiding elements with javascript,hiding div with javascript,hiding javascript

I'm trying to hide a div when the user clicks anywhere outside of the div with id "main".

I am using vanilla Javascript and would prefer not to use jQuery.

The code I have works well but whenever the user clicks inside sub-child div or any

text, it also hides the div, which is not the behavior that I want.

window.addEventListener('mouseup', function(event){
	var box = document.getElementById('main');
	if (event.target != box && event.target.parentNode != box){
        box.style.display = 'none';
    }
});
<div id="main" style="display: block; background-color: grey;">

<div id="one1">
<div id="one2" style="background-color: red;"><p>when click here should not hide<br><br>when click here should not hide<br>when click here should not hide<br>when click here should not hide</p></div>

</div>
<div id="one3"><p>when click here should not hide</p></div>


</div>
<p>when click here should hide</p>
<div id="xyz" style="background-color: green;"><p>when click here should hide</p></div>

<p>when click here should hide</p>
Answer:1

You can add a separate listener on the Main div to stop propagation of that event:

window.addEventListener('click', function(event){
    var box = document.getElementById('main');
    box.style.display = 'none';
});

var box = document.getElementById('main');
box.addEventListener('click', function(event) {
    event.stopPropagation();
});

Fiddle: https://jsfiddle.net/jaredk/bu5cjh9v/

This is from an answer here, but that one uses jQuery.

Answer:2

I'm at a complete loss here. I'll provide what I currently have below, but for some reason, every iteration I try, something goes wrong...either the animation of scroll doesn't work but the other ...

I'm at a complete loss here. I'll provide what I currently have below, but for some reason, every iteration I try, something goes wrong...either the animation of scroll doesn't work but the other ...

Hi i have a link tag that i am creating from javascript. now i want to append a parameters to that link just like below example.so that when the user clicks that button it should go to that url along ...

Hi i have a link tag that i am creating from javascript. now i want to append a parameters to that link just like below example.so that when the user clicks that button it should go to that url along ...

  1. creating link with hyperlink
  2. creating link in html
  3. creating link in linux
  4. creating link in excel
  5. creating link in word
  6. creating link in unix
  7. creating link in word document
  8. creating link in instagram bio
  9. creating link in pdf
  10. creating link to folder
  11. creating link in ubuntu
  12. creating link to pdf file
  13. creating link in windows
  14. creating link in markdown
  15. creating link in excel sheet
  16. creating link in instagram post
  17. creating link in dropbox
  18. creating link in pdf document
  19. creating link in php
  20. creating link in html without underline

I want to know if there is any benefit in passing off db or other async calls to a worker process or processes. Specifically I'm using heroku and postgres. I've read up a good bit on node.js and how ...

I want to know if there is any benefit in passing off db or other async calls to a worker process or processes. Specifically I'm using heroku and postgres. I've read up a good bit on node.js and how ...

  1. using background worker
  2. using backgroundworker c#
  3. use backgroundworker class
  4. using background worker in vb.net

I am making a website in Socket.io. But emit method not working in my code. I can't see any errors in my code. Here is my server code. var io = require("socket.io").listen(server); //Some external ...

I am making a website in Socket.io. But emit method not working in my code. I can't see any errors in my code. Here is my server code. var io = require("socket.io").listen(server); //Some external ...

  1. node socket emit
  2. node js socket emit
  3. node js socket emit example
  4. node socket.io emit
  5. node.js socket.emit callback
  6. node js socket emit error
  7. node js net socket emit
  8. node.js socket.io emit
  9. node.js socket.io emit json
  10. node js socket io emit callback
  11. node.js socket.broadcast.emit
  12. socket.emit node.js not working
  13. node.js socket.volatile.emit