0

This function below works in static pages but in dynamic pages it doesn't work correctly, it executes both functions when clicked the first time.

and when tested with firebug (or jsenav ) it does work correctly

$(document).ready(function() {
    $(".toggleShipingSched").toggle(function(){
    $(this).html("close schedule").addClass("open").parent().animate({height:300+"px"},500);
},
    function(){
    $(this).html("see schedule").removeClass("open").parent().animate({height:34+"px"},500);
})
});

By saying Dynamic I meant that it came from a server generated content. not static HTML And I solved the problem.

1
  • 2
    what do you mean by "static" and "dynamic" pages? Dec 9, 2009 at 22:11

1 Answer 1

4

I'm gonna take a wild guess here. If you by dynamic page mean that you load something with ajax (or generate with javascript) you will have to bind the .toggle() after the content is loaded and not on document ready (because the elements loaded will not be present in document ready). If you do this you might end up binding .toggle() twice.

Basically, if you run $(".toggleShipingSched").toggle() twice it will not override the first event but rather bind another event to it. When you then click the .toggleShipingSched element two functions will be called.

If this is the problem make sure you unbind the click event before you bind the new toggle().

$(".toggleShipingSched").unbind('click');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.