4
What is the best method to hide a div
, that is fulfilling the role of dropdown
, when the user clicks on another page location?
At first, I value solutions cross-browser and without the use of frameworks, but any extra content of applications using jQuery or even CSS will be welcomed.
The selector
$('*')
will match all elements. If you have 1000 elements on the page, it will rotate$('#suaDiv').hide()
1000 times - this not counting the spread of the event, actually will run much more...– bfavaretto
You were wrong, the question the guy asks a solution to close the drop-down when the user clicks on some element, so it will only run if the user clicks on some element of the screen, precisely why I used the function .click()...
– Kenny Rafael
True, I was wrong, it won’t run 1000 times in this case. But consider only the propagation part of the event: if you click a nested div 10 levels into the body, the event will propagate and run 10 times. Also, with this code a click inside the dropdown itself (for example, click the dropdown open arrow) will cause the closure. Note: I’m not the one who voted you down.
– bfavaretto
Solving your remarks I edited my answer, in the case of if for the element that has to open the drop-down there are millions of ways to treat this...
– Kenny Rafael
Okay, but you meant
classeDoElementorFechar
? I still prefer the solution with a single Event Handler and delegation ($('*').click
adds an Handler to each html element).– bfavaretto
It depends on the point of view, well, I did not present my answer claiming to be the best, I did what I would do at first, thankfully that the OS is very democratic and allows the owner of the question to choose the answer that suits you best is not even! ;)
– Kenny Rafael
Yes, no doubt! I was just trying to help, sorry if I seemed aggressive.
– bfavaretto