You're not doing anything in your code to remove the original click
event. To make it work this way, you'd need to make a single recursive
function using .unbind() to remove the first click event, add the
second, and then on the second click, remove the second click and call
itself again to re-bind the first click...

$(function(){
        clickToggle = function(){
                alert('close hit');
                $(this).html('Cancel').unbind('click').click(function(){
                        alert('cancel hit');
                        
$(this).html('Close').unbind('click').click(clickToggle);
                });
        }

        $('#cButton').click(clickToggle);
});


However, another way to accomplish this would be to use jQuery's built-
in .toggle() event to specify the "even" and "odd" actions:

$(function(){
        $('#cButton').toggle(
                function(){
                        alert('close hit');
                        $(this).html('Cancel');
                },
                function(){
                        alert('cancel hit');
                        $(this).html('Close');
                }
        );
});


HTH,
Jason





On Mar 31, 4:38 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> I have a button, when clicked this button invokes a function that
> changes the buttons html, and it's click. The problem is it somehow
> maintains the old trigger code. Before I put the click functions in
> their own functions (rather than just adding function(){} to the
> parameter) it would gain code every click, so if you clicked the
> button 4 times the code would execute both trigger functions 4 times,
> then if you clicked it a fifth time it would do both functions 5
> times.
>
> I can't think of any way to resolves this besides reverting to
> javascript code, and even then I'm not sure if it will work. I looked
> at the jquery source, but couldn't figure out what the triggers code
> was doing. (To me it looked like it was defining a bunch of function
> that took in functions, but with no connection to the javascript
> triggers >>)
>
> Anyways this is my code:
> --
> $(function() {
>    $("#cButton").click(closeButton);
>
> });
>
> function closeButton()
> {
>    alert('close hit');
>    $("#cButton")
>    .html("Cancel")
>    .click(cancelButton);
>
> }
>
> function cancelButton()
> {
>    alert('cancel hit');
>    $("#cButton")
>    .html("Close")
>    .click(closeButton);}
>
> --
> When you hit the button the first time it gives the 'close hit' alert,
> but if you hit it a second time it will give the 'close hit' alert
> first, then give the 'cancel hit' alert.
>
> Any idea?

Reply via email to