Get function work for new loaded elements
Hello,
I am in front of a problem. I know this was discussed in this forum already but this didn't really help me.
I am loading content with $.ajax and I am receiving a bunch of elements. ( In this case "posts" ). Now I wanted to work on and make the like / share function work but the elements don't react to the click. I already tried the .on() thing out. Either I did it wrong or it just does not work. I hope you can help me.
- // Load content
- var first_post;
- var last_post;
- $(document).ready(function(){
- $.ajax({
- type: "post",
- url: "lib/php/database.lib.php",
- data: {action: "loadPosts", limit: 10},
- success: function(returned) {
- $("#main-wrapper .content.right #left #posts #userpost-content").append(returned);
- first_post = $("#main-wrapper .content.right #nbcontent #posts .user-post").first().attr("id");
- last_post = $("#main-wrapper .content.right #nbcontent #posts .user-post").last().attr("id");
- }
- });
- });
-
- $("#main-wrapper .content.right #nbcontent #posts .user-post").on("click", "div.like", function(){
- alert("OK");
- });