0

How can I catch events (.click()) on dynamically inserted DOM elements after page load using jquery?

This is how it not! works:

   $("#test").click(function(){
        $(this).css("background","#CCCCCC");
   });

   $("#clickme").click(function(){
       $("body").append("<div id=\"test\">My background isn't changeable!</div>");
   });

http://jsfiddle.net/d87ckf1k/3/

Philipp
  • 1,001
  • 3
  • 10
  • 10
  • Keep in mind that you are adding more divs that have the same IDs. So change them to class! http://jsfiddle.net/MR_Saberi/d87ckf1k/5/ – M Reza Saberi Jan 07 '15 at 10:56

1 Answers1

0
$(document).on("click","#test",function(){
        $(this).css("background","#CCCCCC");
   });

http://jsfiddle.net/d87ckf1k/4/

Sadikhasan
  • 18,365
  • 21
  • 80
  • 122