Track click on jQuery created button -
Track click on jQuery created button -
this question has reply here:
event binding on dynamically created elements? 13 answersi trying create button on button click , if created button clicked there should action.
here code:
//edit function $('.edit').on('click', function(e){ e.preventdefault(); content = $(this).prop('name'); id = $(this).prop('id'); if(!document.getelementbyid('editnews')) { jquery('#edit').append('</br><textarea id="editnews" value="'+id+'"></textarea></br>');//is not shown, needed stop, damn online lib jquery('#edit').append('<button name="'+id+'" class="saveedit btn btn-success btn-mini"><span class="glyphicon glyphicon-pencil"></span> save</button>'); } $('#editnews').val(content); }); // edit (send) function $('.saveedit').on('click', function(e){ console.log("oke"); e.preventdefault(); $.ajax({ type: 'post', url: 'index.php?page=news', data: {editid:$(this).prop('name'), editcontent:$('#editnews').val()} }).done(function(response) { location.reload(); }); });
so first edit function works fine, sec not button not tracked. hope can help meh
you need utilize event-delegation
in order bind event dynamically created element
$('#edit').on('click','.saveedit', function(e){ console.log("oke"); e.preventdefault(); $.ajax({ type: 'post', url: 'index.php?page=news', data: {editid:$(this).prop('name'), editcontent:$('#editnews').val()} }).done(function(response) { location.reload(); }); });
jquery
Comments
Post a Comment