jquery - How to hide a div by clicking the same toggle button or elsewhere in the document -
right can show div clicking button, , hide using same button. that's working fine, user able click outside of newly shown div or button show it, , still hide it.
hey guys, first time asking question here, i'm sorry if didn't enter code right or something. have button, , want 1 way open hidden div (clicking said button), 2 ways close (clicking same opening button or elsewhere in document).
here current code.
//this part works fine opening , closing, can't click outside button make close once opened.<br/> $(".account").click(function () { $(".accountlinks").toggle(); $(".account").toggleclass("active"); }); $(document).click(function(e) { $('.accountlinks').hide(); $(".account").removeclass("active"); }); $('.accountlinks').click(function(e) { e.stoppropagation(); });
is there way see if opened, , close document.click function using $('.account').hasclass('active');
in way?
thank help.
here's approach: rather giving "active" class button, appear doing, give div. way, can select open divs in document click using "active" class. check out:
$(function() { $("#toggler").click(function(e) { $("#toggled").toggle().toggleclass("active"); e.stoppropagation(); }); $(document).click(function(e) { $('.active').hide().removeclass('active'); }); $("#toggled").click(function(e) { e.stoppropagation(); }); });
and here's demo of that: http://jsfiddle.net/ender/xnqn3/
Comments
Post a Comment