0

I wrote function while clicking a class name. and its works fine. but when i am append a tag with same class name the function is not working on that class.

<script> 
    $(document).ready(function() {

        $("#btn1").click(function(){
            $("p").append(" <b class='test'>Appended text</b>.");
        });

        $(".test").click(function(){
            alert(12);
        });

    });
</script>

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

<ol>
    <li class="test">List item 1</li>
</ol>

<button id="btn1">Append text</button>
Ashwin Parmar
  • 3,025
  • 3
  • 26
  • 42
Neethu
  • 124
  • 2
  • 2
  • 11

1 Answers1

0

That's because jQuery binds the event at the time you call $(".test").click(...). You need to bind it separately:

$("#btn1").click(function(){
    $("p").append(" <b class='test'>Appended text</b>.")
          .click(function(){alert(12);});
});
Andrew Cheong
  • 29,362
  • 15
  • 90
  • 145