Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using jQuery .live with toggle event

Tags:

jquery

My code is working, but requiring me to click twice to active my chaining (once for the click event, and once for the toggle event.) What can I do to make it so I only have to click once so that the toggle will automatically occur?

    //Show or Hide Comments
$('#showHideComments').live('click', function() {
    $('#showHideComments').toggle(function() {
        $(".commentsSwitch").animate({"marginLeft": "+=53px"}, 500);
        $("#comments").fadeIn(300);
        $("#addComment").fadeIn(300);
    },function(){
        $(".commentsSwitch").animate({"marginLeft": "-=53px"}, 500);
        $("#comments").fadeOut(300);
        $("#addComment").fadeOut(300);
    });
});

Thanks!

like image 570
Dave Kiss Avatar asked Jan 31 '10 17:01

Dave Kiss


People also ask

How do I toggle an event in jQuery?

The toggle() method attaches two or more functions to toggle between for the click event for the selected elements. When clicking on an element, the first specified function fires, when clicking again, the second function fires, and so on. Note: There is also a jQuery Effects method called toggle().

What is the use of toggle () method in jQuery?

jQuery toggle() Method The toggle() method toggles between hide() and show() for the selected elements. This method checks the selected elements for visibility. show() is run if an element is hidden.

What is .live in jQuery?

jQuery live() Method The live() method attaches one or more event handlers for selected elements, and specifies a function to run when the events occur. Event handlers attached using the live() method will work for both current and FUTURE elements matching the selector (like a new element created by a script).

What is toggle event?

Definition and Usage The ontoggle event occurs when the user opens or closes the <details> element. The <details> element specifies additional details that the user can view or hide on demand.


3 Answers

You cannot use live and toggle together. What you can do, is simply make your own "toggle" of sorts:

$('#showHideComments').live('click', function() {
    if($("#addComment").is(':visible')){
      $(".commentsSwitch").animate({"marginLeft": "-=53px"}, 500);
      $("#comments, #addComment").fadeOut(300);
    } else {
      $(".commentsSwitch").animate({"marginLeft": "+=53px"}, 500);
      $("#comments, #addComment").fadeIn(300);
    };
});

live is binding to click, however, when toggle is called, it is also bound (normally) on click. You should decide if 'live' is really what you need. For instance, if #showHideComments object is replaced via AJAX during the use of the page, then you need live and my solution. However, if it isn't swapped out and remains the same, just use the inside of your original live function (just the toggle code) and you will be golden.

like image 149
Doug Neiner Avatar answered Oct 11 '22 16:10

Doug Neiner


//Show or Hide Comments
$('#showHideComments').live('click', function() {
    $('#showHideComments').toggle(function() {
        $(".commentsSwitch").animate({"marginLeft": "+=53px"}, 500);
        $("#comments").fadeIn(300);
        $("#addComment").fadeIn(300);
    },function(){
        $(".commentsSwitch").animate({"marginLeft": "-=53px"}, 500);
        $("#comments").fadeOut(300);
        $("#addComment").fadeOut(300);
    }).trigger('click');
});

This will also work :)

like image 25
David M. Avatar answered Oct 11 '22 14:10

David M.


Better yet, use $(this) for the toggle so it refers to the parent - this will work better with multiple instances based on classes or unique objects referred by ID at the parent:

$('#showHideComments').live('click', function() {
    $(this).toggle(function() {
        $(".commentsSwitch").animate({"marginLeft": "+=53px"}, 500);
        $("#comments").fadeIn(300);
        $("#addComment").fadeIn(300);
    },function(){
        $(".commentsSwitch").animate({"marginLeft": "-=53px"}, 500);
        $("#comments").fadeOut(300);
        $("#addComment").fadeOut(300);
    }).trigger('click');
});
like image 2
Dylan Glockler Avatar answered Oct 11 '22 14:10

Dylan Glockler