Hello! I'm trying to build a menu with drop-down blocks and I have a little difficulty.
The principle is this, from HTML we take the attribute data in which the id of the block is registered, which we hide / display through jQuery. Two questions: how to prevent hiding a .navbar-dropdown-content block when clicking on itself (it closes because it is a child of the block I set this function for) and how to organize block concealment when clicking on any other place on the page?
<span class="navbar-dropdown" data-dropdown-id="navbar-dropdown-user"> Первая кнопка <div class="navbar-dropdown-content" id="navbar-dropdown-user"> Контент </div> </span> <span class="navbar-dropdown" data-dropdown-id="navbar-dropdown-user2"> Вторая кнопка <div class="navbar-dropdown-content" id="navbar-dropdown-user2"> Контент 2 </div> </span> (function ($) { 'use strict'; $(document).ready(function(){ $(".navbar-dropdown").click(function(){ var link = $(this); var dropdown_id = "#" + $(link).data("dropdown-id"); $.fn.dropdownOn = function() { $(link).addClass("is-active"); $(dropdown_id).show(); $(".navbar-dropdown").not(link).removeClass("is-active"); $(".navbar-dropdown-content").not(dropdown_id).hide(); }; $.fn.dropdownOff = function() { $(link).removeClass("is-active"); $(dropdown_id).hide(); }; if ($(link).hasClass("is-active")) { $(this).dropdownOff(); } else { $(this).dropdownOn(); }; }); }); })(jQuery);