javascript - Issue with .on() method -
javascript - Issue with .on() method -
i trying create mvc training purposes , next tutorial rather old. implementation in tutorial made using live() decided utilize jquery 2.1.1 , have implement on() method. made little utilize case clarification.
i can insert new elements on page while adding them in db i can delete preloaded elements existed in db @ page load both db , dom i can not remove elements added live neither db nor dom.this entire code regarding that.
$(function(){ $.get('dashboard/xhrgetlistings', function(o){ (var = 0; < o.length; i++ ) { $('#listinserts').append('<div>' + o[i].text + '<a class="del" rel="' + o[i].id + '" href="#">x</a></div>'); } $('.del').on("click", function() { delitem = $(this); var id = $(this).attr('rel'); $.post('dashboard/xhrdeletelisting', {'id': id}, function(o) { delitem.parent().remove(); // not executed @ }, 'json'); homecoming false; }); }, 'json');
//not necesarly relevant, helps code clarity
$('#randominsert').on("submit", function() { var url = $(this).attr('action'); var info = $(this).serialize(); console.log(data); $.post(url, data, function(o) { $('#listinserts').append('<div>' + o.text + ' <a class="del" rel="' + o.id + '" href="#">x</a></div>'); }, 'json'); homecoming false; });
});
another issue i'm not focussing on @ point if delete parent within $.post method (as shown in code above) it's not deleted, if move line outside of post method. clarification on appreciated.
use event delegation
, event.preventdefault()
stops default action
$('#listinserts').on("click", '.del' , function(event) { event.preventdefault(); // code come here });
javascript jquery
Comments
Post a Comment