Hide - jquery toggle show div

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I have a chat feature on my site, and users click a button to open it

$(function() { 
$( #chat ).hide(); // hides the chat on load

$("#chat_btn").click(function(){ 
    $( #chat ).show();
    $( #chat ).load("chat.php");
    }); 
});

This all works fine, but I want to be able to click the same button to close the chat too.

I know there s some kind of toggle function instead of hide and show, but I want the other command (load the chat.php) to run only on the open.

How can I do this?

Answers

http://api.jquery.com/toggle-event/ http://api.jquery.com/toggle-event/

$("#chat_btn").toggle(function(){ // run the first time
    $( #chat ).show();
    $( #chat ).load("chat.php");
}, function() { // run the second time
    $( #chat ).hide();
});

NB that you could make this (a tiny bit) more efficient by chaining the show and load calls:

    $( #chat ).show().load("chat.php");

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/10302947/jquery-toggle-show-div

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils