[Answer]-Django javascript for dynamic html table cells

1👍

Try this:

html

<td><div class="clicks"><a class="click-to-show" href="#" data-desc="{{ i.description }}">click to show</a></div></td>

javascript

$(function(){
   $('.click-to-show').click(function(e) {             
       $.ajax({     
           type: "POST",   
           url: "/myapp/test_ajax/", 
           data: {   
               'ajax_data' : $(e.target).attr("data-desc"), 
               'csrfmiddlewaretoken' : $("input[name=csrfmiddlewaretoken]").val() 
           },
           success: function ajaxSuccess(data, textStatus, jqXHR) {
               $(e.target).parent().html(data);
           }, 
           dataType: 'html'
       });

   });

});

As you can see we keep the DOM relative to link element all the time, since we render rows multiple times, this will make sure we update the correct row.

Leave a comment