Search code examples
javascriptjqueryevent-propagation

Reassigning the click event from click (using jQuery)


I'm having some trouble with this javascript (using jQuery):

$(a).click(function(){
    alert('hi')
    this.unbind.click(function(){
        doSomethingElse(this);
    })
})

When a is clicked, it is supposed to reassign its own click event. Unfortunately, it fires the second click event as well. I know this is to do with propagation, but I can't work out how to stop this behaviour.

Any ideas?

Sam


Solution

  • $(a).click(function(e){
        alert('hi')
        $(this).unbind('click', arguments.callee).click(function(){
            doSomethingElse(this);
        })
        e.preventDefault();
    })