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
  • 891
  • 3
  • 9
  • 9
  • 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/ – 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
  • 17,858
  • 20
  • 77
  • 117