0

I have some code that allows the user to create divs with some content in them on a page. These divs are dynamically generated and I need a click event that fires when a user clicks on any one of the dynamically generated divs.

All the dynamically created divs have class="entry".

<div class="entry">
//some content
</div>
<div class="entry">
//some other content
</div>
//...

Following the answer given here my code looks like this:

$('body').on('click','div.event',function(){
    console.log("clicked");
    //do stuff
});

However this event does not seem to fire as console.log("clicked") is never executed. What am I doing wrong and how do I fix this?

EDIT: Sorry guys it was just the typo. I cannot believe I didn't spot that before. So sorry for wasting your time, unfortunately I cannot delete the question now that people have answered it.

Aswin G
  • 361
  • 3
  • 9
  • 18

2 Answers2

5

You wrote event instead of entry.

$('body').on('click','div.entry',function(){ console.log("clicked"); //do stuff });

Damien
  • 1,582
  • 1
  • 13
  • 24
  • @jbman223 he is not getting downvoted, it was another guy who was getting downvoted, but he deleted his answer. The answer was actually the exact same one as Damien's answer, and also the exact same as the comment in the question. I guess it was downvoted because some1 thought: "hey he just copied that from the comments", which was not the case most likely. Yeah. – Freeman Lambda Jun 09 '17 at 12:29
  • 2
    Oh right it was you @prasad :). Too bad you got that downvote, a correct answer is a correct answer, no matter how trivial. – Freeman Lambda Jun 09 '17 at 12:34
2

Change your code to below. it should work.

$('document').on('click','.entry',function(){
    console.log("clicked");
    //do stuff
});

document will search your class name in entire document,once it finds the class name,then event gets fire.

gajju_15
  • 527
  • 4
  • 17