I have a web page, with an index that slides in from the side. What I want, is that when I click anywhere except the index object, the index should close. To solve this, I first need to be able to select all elements except for the index, but I'm having a hard time doing this, because I have to use event delegation (I have dynamically loaded DOM elements).
I understand this may have been asked before, but the solutions in the other answers do not work for me since I'm using event handling, and I don't have enough of an understanding of jQuery to figure out how tho implement their answers.
I tried this, but it hasn't worked out for me:
$(document).on('click','*:not(.index)',function(e) {
alert("clicked")
return false;
});
The complete html code (minified) is this:
<html>
<div class="header" id="messenger">
<div class="header-text">
<span>Dodecahedron-n</span>
</div>
</div>
<div class="topnav">
<a href="#" class="link home-btn">Dodecahedron-n</a>
<a href="http://pythonrepo.rf.gd/?i=1" class="link">Scripts</a>
<a href="#" class="link index-btn">Index</a>
<a href="#" class="link sort-btn">Sort: Newest</a>
<a href="#" class="link menu-btn"><i class="fa fa-bars"></i></a>
</div>
<div class="no-click index">
<div class="no-click index-heading">
<span class="no-click vertical-center">Index</span>
</div>
<div class="no-click scroll-down-prompt">
<span class="no-click vertical-center">Scroll Down <i class="fa fa-level-down"></i></span>
</div>
<div class="no-click wrapper">
<div class="no-click index-content"></div>
</div>
<div class="no-click index-close-btn-container">
<span class="no-click vertical-center"><a href="#" class="index-close-btn">Close <i class="fa fa-close"></i></a></span>
</div>
</div>
<div class="index-tab">Index <i class="fa fa-caret-square-o-right" style="color: #F45C43"></i></div>
<div class="page"></div>
<div class="footer">
<span class="main">Dodecahedron-n</span>
<span class="side">Copyright <i class="fa fa-copyright"></i> Aarush.K 2019</span>
</div>
<script>
$(function(){
$(document).on('click','*:not(.index > *)',function(e) {
alert("clicked")
return false;
});
});
</script>