0
how to close sidenavbar onclick of outside
I have made sidebar using bootstrap now I need to close that sidebar by clicking outside currently it's closing onclick of hamberger plz anyone tel me. Thanks in advance
3 odpowiedzi
+ 1
Krishnaji Kulkarni [edit]
in addition to Runtime Terror answer, I would also test if event target is not your sidebar or any else element part of it (to avoid closing on click inside) as onclick event was propagated in the dom tree hierarchy ^^
also, 'window' could be avoided, as implicitly passed as 'this' argument if not specified ;P
const sidebar = document.querySelector(/* here come the query string wich target the side bar element, in css selector format */);
addEventListener("click",evt => {
if (
/* side bar is opened */ &&
!sidebar.contains(evt.target)
) {
/* close/hidde side bar */
}
});
[edit] forgotten required 'document' target of 'querySelector': corrected...
0
Show us your code
0
Thank you