0

I am trying to add a callback function on click (when user clicks the button second time).

I have

$('#toggle').live('click', function(){
        $this=$(this);
        $this.attr('id', 'detail')
        turn()
    }, function(){
        $this.attr('id','detail_close')
        turn()
    })

I can't seem to change the id to detail_close and call turn() again after the second click. Any tips? Thanks a lot!

2 Answers 2

1
   var count = 0; // a counter
   $(document).on('click', '#toggle', function(){

        $this = $(this);
        $this.prop('id', 'detail');  // may use prop(), instead of attr()

        count++;  // increase counter value

        // checking for counter value
        if(count > 1) {
             // do something on second click

             $this.prop('id', 'detail_close'); // change id
             turn();  // call turn()

             count = 0; // reset the counter
        }
    });

As .live() is deprecated, so in order to make delegate event handling use .on() method like:

$(StaticParent).on(eventName, target, handlerFunction)
Sign up to request clarification or add additional context in comments.

1 Comment

I can't use on becasue I am using Jquery 1.64
0
$(document).on('click', '#toggle', function(){
    $this=$(this);
    if($this.prop('id') == 'detail'){
        $this.prop('id', 'detail_close');
        turn();
    }else{
        $this.prop('id', 'detail');
        turn();
    }
});

We use .on and .prop in the newest version of jQuery.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.