(function ($) { $.fn.dropDown = function (ajaxCallback) { $(this).children("a").each(function (i) { $(this).parent().click(function (e) { e.stopPropagation(); }); $('body').on('click', '.dropDown-items', function() { var dd = $(this); dd.find("a").click(function () { // Close dropdown when link clicked hideDropDown(dd); }); }); $(this).click(function (e) { //Close other dropdowns $(".dropDown-items").each(function (index, element) { hideDropDown($(element)); }); var a = $(this); if (a.attr("href") == "#") { var dd = a.next(".dropDown-items"); if (a.hasClass("open") == false) { showDropDown(dd); } else { hideDropDown(dd); } } else { if (ajaxCallback) { ajaxCallback(function () { a.attr("href", "#"); showDropDown(a.next(".dropDown-items")); }); } else { $.get($(this).attr("href"), function (data) { a.attr("href", "#"); showDropDown(a.after(data).next()); }); } } return false; }); function showDropDown(dd) { var a = dd.prev("a"); a.addClass("open").attr('aria-expanded', true); a.html(unescape(escape(a.html()).replace('u25BE', 'u25B4'))); dd.attr('aria-hidden', false).slideDown(200); dd.find('a').click(function (e) { //hideDropDown(dd); }); $('body').bind('click.dropDown' + i, function (event) { hideDropDown(dd); }) .bind('keyup.dropDown', function (event) { if (event.which == 27) { hideDropDown(dd); } }); } function hideDropDown(dd) { $('body').unbind('click.dropDown' + i); $('body').unbind('keyup.dropDown'); dd.attr('aria-hidden', true).slideUp(100, function () { var a = dd.prev("a"); a.removeClass("open").attr('aria-expanded', false); a.html(unescape(escape(a.html()).replace('u25B4', 'u25BE'))); }); } }); } })(jQuery);