Results 1 to 2 of 2

Thread: unbind bind button click

  1. #1
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default unbind bind button click

    i have a button which i would like to disable click until a certain action finishes
    my code so far doesnt animate
    i have tried different configurations but cant seem to get it to work
    what am i doing wrong here?
    Code:
    <!DOCTYPE HTML>
    <html lang="en-US">
    <head>
    <style type="text/css">
    div{float:left; margin-right:50px;}
    #rec{position:relative;}
    </style>
    <script src="http://code.jquery.com/jquery.min.js"></script>
    <script type="text/javascript">
    var num = 0;
    var arr = [];
    $(function(){
    	
    	$('#btn2').click(function() {
    	
    		$(this).unbind('click', function(){
    			$('#rec').animate({left:'+=50'}, 500, function(){
    				$('#btn2').bind('click');
    			});
    		
    		});
    		
    		num++;
    		$('#rec').html(num);
    		return false;
    	});
    	
    });
    </script>
    </head>
    <body>
    <div id="btn2"><a href="">right</a></div>
    <div id="rec">0</div>
    </body>
    </html>
    this animates but the bind doesnt work
    Code:
    $('#btn2 a').click(function() {
    
    	$(this).unbind('click');
    	$('#rec').animate({left:'+=50'}, 500, function(){
    		$('#btn2 a').bind('click');
    	});
    });
    Last edited by ggalan; 02-12-2012 at 09:28 PM.

  2. #2
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    got it to work like this
    Code:
    var btnR = $('#btn2 a');
    	var recDiv = $('#rec');
    	var handler = function() {
    		btnR.unbind('click', handler);
    		recDiv.animate({left:'+=50'}, 500, function(){
    			btnR.bind('click', handler);
    		});
    			
    		num++;
    		recDiv.html(num);
    		return false;
    	}
    
    	btnR.bind('click', handler);

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •