How to used Java script delegate, with each function value inside the click event?

I used JS delegate and working. And now i want to use JSON success $.each value inside the click event?

$(window).load(function() {
  $.ajax({
    url: '<?php echo base_url(); ?>' + 'main/data',
    dataType: "JSON",
    type: "POST",
    success: function(retdata) {
      $.each(retdata, function(i) {
        var content = '<div class="target btn-default">' + retdata[i].content;
        $("#article-data").append(content);
      });
    }
  });
});
//Using event delegate
$(function() {
  //Display dialog box when mouse click
  $("#article-data").on('click', ".target", function() {
    // retdata[i].content;                    
  });
});
<div id="article-data">

</div>

Answers 1

  • You can save data with jQuery:

    $(window).load(function() {
      $.ajax({
        url: '<?php echo base_url(); ?>' + 'main/data',
        dataType: "JSON",
        type: "POST",
        success: function(retdata) {
          $.each(retdata, function(i) {
            var content = $('<div class="target btn-default"/>');
            content.data('retdata', retdata);
            content.html(retdata[i].content);
            content.appendTo($("#article-data"));
          });
        }
      });
    });
    
    //Using event delegate
    //Display dialog box when mouse click
    $("#article-data").on('click', ".target", function() {
        var data = $(this).data('retdata');
        console.log(data);                  
    });
    

Related Articles