Search code examples
javascriptjqueryunbind

jquery unbind click doesn't work


Please give me a hand.Thanks in advance! Here is the simple coding:

var $newbox = $( "<div/>" );
$newbox.addClass("init_box");
for(i=0; i<3; i++) {
    $("#game").append($newbox.clone().click(function(){
        $(this).addClass("select_box"); 
        $("div.init_box").unbind("click");
    }));
}

I wanna create 3 divs and once any one of this 3 divs is clicked, then others will unbind the click event. But the code above doesn't work.


Solution

  • You need to bind the click handler using .bind() if you are going to use .unbind();