0

The callbacks functions are not working on the elements which are added by JQuery. Here in my example there is a parent div with class=parent. After clicking on it a <span class="child">Child</span> is being added. But when I am click on the the child it does not alert the message. Please see the example below

    <html>
    <head>
        <style>
            .parent{
                width: 50px;
                height: 30px;
                line-height: 30px;
                background-color: #232323;
                color: #fff;
                text-align: center;
            }

            .child{
                width: 50px;
                height: 30px;
                line-height: 30px;
                background-color: #f3b556;
                color: #232323;
                text-align: center;
                margin-top: 10px;
            }
        </style>
    </head>
    <body>
        <div class="parent">
            <span>Parent</span>
        </div>


        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script>
            $('.parent').click(function(){
                alert('parent clicked');
                $(this).after("<span class='child'>child</span>");
            });
            $('.child').click(function(){
                alert('child clicked');
            });
        </script>
    </body>
</html>
Shan Biswas
  • 397
  • 2
  • 8
  • 24

3 Answers3

0

Use a live delegation, because the .child is dynamic added element a normal event listener will not register the click on childs generated in the future.

$("body").on("click", ".child", function(){
    alert('child clicked');
});
eisbehr
  • 12,243
  • 7
  • 38
  • 63
0

You can delegate the event from the parent.

When you create a new object, it lacks event listeners that could be added before to other elements with it's same class.

However, if you add the listener to an existing inmutable parent, it will work always:

$("#existingContainer").on("click", ".child", function() {
    //whatever
})

In this way, the event is captured on #existingContainer, but the callback is executed only when it's captured inside a ".child" object that lies inside #existingContainer, no matter if this .child object was added after the event listener was created.

Bardo
  • 2,470
  • 2
  • 24
  • 42
-1

As the dom manuplation is occuring, and it passes through to the $('.child').click(function()) function, it did not find any respective element at DOM to manipulate. As you adding this element dynamically, you have to bind/ register the event after dynamically adding the class to register the event to be listen on it. you can do this by

 $('.parent').click(function(){
                alert('parent clicked');
                $(this).after("<span class='child' onclick='ChildelementClick()'>child</span>");
            });
function ChildelementClick(){
// do somehing
}

Or you can use the example given by @eisbehr

Nalin Aggarwal
  • 886
  • 5
  • 8