I have my dropdown, it is working on click of its parent anchor tag, but now i want to close it on body click
I have try to do it with event.stopPropagation(); but it didn't work, here is my code -
$(".mydropd > a").click(function() { $(this).siblings(".mycustom_dropd").slideToggle(); }); $('body').click(function(event) { $(".mycustom_dropd").slideUp(); event.preventDefault(); event.stopPropagation(); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <li class="mydropd"> <a href="#" class="mydropd_a">menu <span class="line1"></span> <span class="line2"></span> <span class="line3"></span> </a> <ul class="mycustom_dropd"> <li> <a href="#"><img src="images/vibgyor.png"> VIBGYOR</a> </li> <li><a href="#">MULTIPLAYER</a></li> <li><a href="#">RAPTOP</a></li> <li><a href="#">HELP</a></li> <li><a href="#">PROFILE</a></li> </ul>
bodywouldn't be having that height. So just try givingheight:100vhandwidth:100vhtobodyand then try clicking.. Your code works with that.. or else try attaching it todocumentinstead ofbodybut that might lead to some other issues.