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

31st May 2021, 4:59 AM
Krishnaji Kulkarni
Krishnaji Kulkarni - avatar
4 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...
31st May 2021, 2:08 PM
visph
visph - avatar
0
Show us your code
31st May 2021, 5:35 AM
Ayush Kumar
Ayush Kumar - avatar
0
Thank you
31st May 2021, 2:13 PM
Krishnaji Kulkarni
Krishnaji Kulkarni - avatar