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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 400
    Comment on it

    live() method:

    Live method is used to attach events to selected elements and also used to specify a function when the event occurs. It is similar to bind method but live method also attach event handler to elements generated using script while bind cannot.

    Syntax:

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

    Example:

    <!DOCTYPE html>
    <head>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
      <style>
        .content{border:1px solid #333;padding:24px 7px;width:250px;margin-bottom:10px;}
        .addlnk{text-indent:40px;}
      </style>
      
    </head>
    <body>
        <div id="main-container">
            <p class="addlnk"><a href="javascript:void(0);" onclick="addDiv();">Add box</a></p>
            <div class="content">
              Use of live() method
            </div>
        </div>
    
        <script type="text/javascript">
        $(function()
        {
                $(".content").live("mouseover", function()
                {
                        $(this).css("background-color", "grey");
                }).live("mouseout", function()
                {
                        $(this).css("background-color", "white");
                });
        });
    
        function addDiv()
        {
                var div = $("<div></div>").addClass("content").text("Content generated using script");
                $("#main-container").append(div);
        }
        </script>
    </body>
    </html>
    http://www.jquery-tutorial.net/events/the-live-method/

    In the above example when we click on add box , a div will get added inside main-container div and also the mouseover and mouseout event will  get attached to new added div.

    The live() method was deprecated in jQuery version 1.7  and now we can use on() method for the same functionality.

 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: