1
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>jQuery UI Button - Icons</title>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <link rel="stylesheet" href="/resources/demos/style.css">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <style>
        #s
        {
            width:200px;
        }
        #div1
        {
            min-width:100px;
            width:auto;
            float:left;
            border:2px solid black;
            min-height:100px;
            height:auto;
            background-color:red;
        }
    </style>
    <script>
        $(document).ready(function(){
            $("div").click(function(){
                $(this).after($('<div id="s" style="background-color:blue;">NLC TRANSPORT</div>'));
            });
            $("#s").click(function(){
                alert("sljsdf");
            });
        });
    </script>
</head>
<body>
    <div id="div1">
        VOLVO B9R
    </div>
</body>
</html>

Here when i click on the first div, the program creates another div. the newly created div is not responding to on click event. in the current code i have specified that the newly created div should give an alert on click but its not working. Thanks!

Blundering Philosopher
  • 5,116
  • 2
  • 35
  • 50

9 Answers9

2

Since the #s element are created dynamically you need to use event delegation to register event handlers to these elements.

When you use $('#s').click(....); to register an event handler it will register the handle to only those elements which are already present in the dom at the time of the code execution, in you case since these elements are created after that the handlers will not get attached to the newly created elements

Try this

$(document).ready(function(){
 $(document).on('click',"div",function(){
  $(this).after($('<div id="s" style="background-color:blue;">NLC TRANSPORT</div>'));
 });
 $(document).on("click", "#s", function(){
   alert("sljsdf");
 });
});

DEMO

Sridhar R
  • 19,414
  • 6
  • 36
  • 35
2

You need event degation for dynamically added elements. The time the binding code for element with id s is executed the element does not exists in DOM

$(document).on("click", "#s", function(){
   alert("sljsdf");
});

Delegated events

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers, jQuery doc.

Adil
  • 139,325
  • 23
  • 196
  • 197
2

When your dom ready handler is executed the #s div does not exists in the dom, so the click handler will not get added to the element. You can use event delegation to fix this problem

$(document).on('click', "#s", function () {
    alert("sljsdf");
});

Demo: Fiddle

Arun P Johny
  • 365,836
  • 60
  • 503
  • 504
2

try something like this

$("body").on('click','#s',function(){
   alert("sljsdf");
});

REFERENCE

http://api.jquery.com/on/

rajesh kakawat
  • 10,330
  • 1
  • 18
  • 38
1

Please see this LINK

OR

Try below code...

Your HTML ....

<div id="div1"> VOLVO B9R </div>

Your JQuery Code....

$(document).ready(function () {    
    $("div").click(function () {    

        $(this).after($('<div id="s" style="background-color:blue;">NLC TRANSPORT</div>'));    

        $("#s").bind('click', function () {
            alert("sljsdf");
        });
    });
});

Your CSS....

#s
{
width:200px;
float:right;
}
#div1
{
min-width:100px;
width:auto;
float:left;
border:2px solid black;
min-height:100px;
height:auto;
background-color:red;
}
Vishal Patel
  • 920
  • 5
  • 11
1

The following jQuery lines of code will help you..

$(document).ready(function(){
    $('body').on('click','#div1',function(){
      $(this).after($('<div id="s" style="background-color:blue;">NLC TRANSPORT</div>'));
      $('#s').click(function(){
       alert('a');
      })
    })
});
marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Green Wizard
  • 3,001
  • 4
  • 14
  • 28
1

Use below code..

 $(document).ready(function () {
        $("div").click(function () {
            $(this).after($('<div id="s" style="background-color:blue; clear:both">NLC TRANSPORT</div>'));
            $("#s").bind("click", function () {
                alert("sljsdf");
            });
        });

    });

Good luck.

Pradeep Pansari
  • 1,287
  • 6
  • 10
0

You can't add an event handler to an element that doesn't exist yet:

 $(document).ready(function () {
     $("div").click(function () {
         $(this).after($('<div id="s" style="background-color:blue;">NLC TRANSPORT</div>'));
         $("#s").click(function () {
             alert("sljsdf");
         });
     });
 });
Korikulum
  • 2,359
  • 19
  • 24
0

Try This Code :

$(document).ready(function(){
 $("#div1").click(function(e){
    $(this).after($('<div id="s" style="background-color:blue;">NLC TRANSPORT</div>'));
     $("#s").click(function(){
         alert("sljsdf");
         });

    });

});
Priya jain
  • 725
  • 2
  • 5
  • 15