Matteo Busacca
Matteo Busacca

Reputation: 43

New divs with javascript

I have a problem with my JavaScript code. In practice I have a function that starts with the click of a div, through ajax and then I query the database and dynamically add the div elements with append() to the main div without reloading the page. Now, I want the JavaScript code to read me also the new div elements added by ajax, how can I do without reloading the page?

  $(".cc > div").on('dbltap', function(){
  var taskid = $(this).data('id');
  var type = $(this).data('status');
  var device = $(this).data('device');

  if(type == 0){
    var status = "done";
    $(this).data('status', 1);
  }
  else{
    var status = "undone";
    $(this).data('status', 0);
  }

  done_undone(taskid, status, device);
})

Upvotes: 1

Views: 68

Answers (1)

Pavlo
Pavlo

Reputation: 44957

I believe you need to delegate:

$(".cc").on("dbltap", "[data-id]", function(){
  // ...
})

See more in jQuery docs: https://api.jquery.com/on/#direct-and-delegated-events

Upvotes: 1

Related Questions