-1

I wrote somthing like this:

<body>
  <div class="item">An Item</div>
<body>

I created this with a jQuery event.

$("button").click(function(){
  $(body).append("<div class="item>An Item</div>");
});

so now i have something like this:

<body>
  <div class="item">An Item</div>
  <div class="item">A New Item Created With jQuery Event</div>
</body>

The problem: I want to do this, to get the info from both divs.

$(".item").click(function(){

  $(this).text();

});

Works fine with the original .item from the HTML. But it doesnt work with the newly created element with jQuery. Why is that?

FYP 89
  • 3
  • 5

1 Answers1

0

use .on('click')

$(document).on('click','.item',function(){

 console.log ($(this).text());

});
Mohamed-Yousef
  • 23,946
  • 3
  • 19
  • 28
  • Wow, that's great. Thank You. Sorry about this being a duplicate post, I did't know how to search for it. – FYP 89 May 05 '15 at 05:47
  • @FYP89 don worry man .. and it will be better if you take a look at the duplicated question as well .. good luck to you – Mohamed-Yousef May 05 '15 at 05:50