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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 362
    Comment on it

    on() method:

    On() 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 on() method also attach event handler to elements generated using script while bind cannot (means it can bind events to both current and future generated elements).




    <!DOCTYPE html>
      <script src=""></script>
        .content{border:1px solid #333;padding:24px 7px;width:250px;margin-bottom:10px;}
        <section id="main-container">
            <p class="addlnk"><a href="javascript:void(0);" onclick="addDiv();">Add box</a></p>
            <div class="content">
              on() method example
        <script type="text/javascript">
                $("#main-container").on("mouseover",'.content', function()
                        $(this).css("background-color", "#5AB0A6");
                        $(this).css("background-color", "white");
        function addDiv()
                var div = $("<div></div>").addClass("content").text("Content generated using script");

    In the above example, when an add box is clicked by the user then a div will get added inside main-container div and also the mouseover and mouseout event will get attached to the div generated using script.

    On() method was replacement for live() method which was deprecated in jQuery version 1.7 and removed in jQuery version 1.9.

 0 Comment(s)

Sign In

Sign up using

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: