Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • delegate and undelegate method in jquerry

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 353
    Comment on it

    delegate() method attach a handler to one or more events for all elements that match the selector, now or in the future, based on a specific set of root elements. The undelegate() method is a way of removing event handlers that have been bound using delegate()

    <!doctype html>
    
    <html lang="en">
    
    <head>
    
    <meta charset="utf-8">
    
    <title>undelegate demo</title>
    
    <style>
    
    button {
    
    margin: 5px;
    
    }
    
    button#theone {
    
    color: red;
    
    background: yellow;
    
    }
    
    </style>
    
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
    
    </head>
    
    <body>
    
    <button id="theone">Does nothing...</button>
    
    <button id="bind">Bind Click</button>
    
    <button id="unbind">Unbind Click</button>
    
    <div style="display:none;">Click!</div>
    
    <script>
    
    function aClick() {
    
    $( "div" ).show().fadeOut( "slow" );
    
    }
    
    $( "#bind" ).click(function() {
    
    $( "body" )
    
    .delegate( "#theone", "click", aClick )
    
    .find( "#theone" ).text( "Can Click!" );
    
    });
    
    $( "#unbind" ).click(function() {
    
    $( "body" )
    
    .undelegate( "#theone", "click", aClick )
    
    .find( "#theone" ).text( "Does nothing..." );
    
    });
    
    </script>
    
    </body>
    
    </html>

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: