0

Here is my ajax function that has var template variable.

$.ajax({
type: "GET",
url: "/Home/GetComment",
dataType: "JSON",
success: 
    function (comments) {
        for (var i = 0; i < comments.length; i++)
        {

            var template = '<a id="like_button" class="like-comment" href="#">Like <span class="glyphicon glyphicon-thumbs-up"></span></a>'
        }

    }
});


$("#like_button").click(function (evt) {
    $.ajax({
        type: "POST",
        url: "/Home/AddLikes",
        data: { "likeid": id },
        dataType: "JSON",
        success:
            function () {
                alert('alert');
            }
    });
});

This click function is does not work with this template, but if I take the same template and put it in my Index view, then the click function works.

This is the code in Index view where it has the same id as the one in the template:

<a id="like_button" class="like-comment" href="#">Like <span class="glyphicon glyphicon-thumbs-up"></span></a>
1
  • Good old event delegation. Commented Mar 31, 2014 at 12:08

5 Answers 5

1

delegate the event :

$(document).on('click', "#like_button", function (evt) {

and change your var id to class:

var template = '<a class="like-comment like_button" href="#">Like <span class="glyphicon glyphicon-thumbs-up"></span></a>'

As your anchor is dynamically generated so this is not directly available in the document to bind the direct event to it, so to overcome this issue

you have to find out the closest static parent of this element which could be a div with some specific class assigned to it or $(document), $(document.body) which is always available to delegate the event.

Sign up to request clarification or add additional context in comments.

Comments

0

I think, currently you've duplicated id, you should use class instead for your button:

var template = '<a class="like_button" class="like-comment" href="#">Like <span class="glyphicon glyphicon-thumbs-up"></span></a>'

Then you can use event delegation to attach events for dynamically loaded elements:

Event delegation allows us to attach a single event listener, to a parent element, that will fire for all children matching a selector, whether those children exist now or are added in the future.

$('body').on('click','.like_button',function() {
    // Your code here
});

Comments

0

When jQuery function that defines click on the method executes, button is not yet created! You have to add click method to element that is already there (body is always there).

So function below reads as: When you click on body, find like_button (if it is there) and click it (delegate event to it) :).

$("body").on("click", "#like_button", function (evt) {
    $.ajax({
        type: "POST",
        url: "/Home/AddLikes",
        data: { "likeid": id },
        dataType: "JSON",
        success:
            function () {
                alert('alert');
            }
    });
});

Comments

0

Try this

$("body").on("click", "#like_button", function (evt) {
    $.ajax({
        type: "POST",
        url: "/Home/AddLikes",
        data: { "likeid": id },
        dataType: "JSON",
        success:
            function () {
                alert('alert');
            }
    });
});

Comments

0

html code:

 <div id="cntr"></div>

js code:

$(document).ready(function(){
$.ajax({
   type: "GET",
   url: "/Home/GetComment",
   dataType: "JSON",
   success: function (comments) {
       var template = '';
       for (var i = 0; i < comments.length; i++) {

            template += '<a id="like_button" class="like-comment" href="#">Like     <span class="glyphicon glyphicon-thumbs-up"></span></a>'
        }

       $('#cntr').html(template);
       addEvent();
   }
  });

 function addEvent() {
    $("#like_button").on('click', function (evt) {
      $.ajax({
          type: "POST",
          url: "/Home/AddLikes",
          data: { "likeid": id },
          dataType: "JSON",
          success: function (d) {
              alert('Here');
          }
      });
   });
  }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.