Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • jQuery bind() Method

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 244
    Comment on it

    Hello Readers,

    bind() is the jquey method which is used to attaches one or more event handlers for selected elements and specifies a function to run when the event occurs.

    syntax:

    $(selector).bind(event,data,function)
    

    event: event is a required field which specifies one or more events to attach to the elements.

    data: data is optional which specifies additional data to pass along to the function.

    function: function is a required field which specifies the function to run when the event occurs.

    Example:

    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>
    function handlerName(e){
        alert(e.data.msg);
    }
    $(document).ready(function(){
        $("p").bind("click", {msg: "You just clicked me"}, handlerName)
    });
    </script>
    </head>
    <body>
    
    <p>Click me</p>
    
    </body>
    </html>
    

    Pros and Cons of jquery bind() method

    Pros

    1. This methods works across various browser implementations.
    2. It is pretty easy and quick to wire-up event handlers.
    3. The shorthand methods (.click(), .hover(), etc...) make it even easier to wire-up event handlers.

    Cons

    1. The method attaches the same event handler to every matched element in the selection.
    2. It doesn't work for elements added dynamically that matches the same selector.
    3. There are performance concerns when dealing with a large selection.

 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: