javascript - how to collapse my div panel in jquery -


when click on outside expanded div div should close.

i have html code like

<div id="panel"> <div>                    <h1>welcome jquery demo</h1>                 <h3>welcome jquery demo</h3>                 <h3>welcome jquery demo</h3>                 <h3>welcome jquery demo</h3>                 <h3>welcome jquery demo</h3>             </div> </div> <div class="tab">             <ul class="loginleft">             <button class="sc-button g-opacity-transition sc-button-large loginbutton" tabindex="0">log in</button>          </ul>         <ul class="login">             <li class="left">&nbsp;</li>             <li>hello guest!</li>             <li class="sep">|</li>             <li id="toggle">                 <a id="open" class="open" href="#">more info</a>                 <a id="close" style="display: none;" class="close" href="#">close panel</a>                      </li>             <li class="right">&nbsp;</li>         </ul>      </div>  

js code

$(document).ready(function() {      // expand panel     $("#open").click(function(){         $("div#panel").slidedown("slow");      });       // collapse panel     $("#close").click(function(){         $("div#panel").slideup("slow");      });            // switch buttons "log in | register" "close panel" on click     $("#toggle a").click(function () {         $("#toggle a").toggle();     });       }); 

how can close expanded div when clicked outside area also.

you can add handler document since click event not "flooding" 1 ...

=> test whether event.target '.close'

$(document).on('click','.close', function(eventobject){   $('#panel').slideup("slow"); // useful if want put several buttons on content }); 

=> or event.target outside panel

jsfiddle here

(try not attach many greedy handlers document keep browser reacting fast.)


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -