Im writing a small app, and im having problems deleting (as in fading out) a dynamically loaded div using jquery.
The problem only presents itself when i add a new content box, if i reload the page and the content box gets rendered not dynamically but normally (as in from the database query), it deletes just fine(as in fade out), but when the div is added anew, i cant get it out.
$result = '<div class="my-music-item">'. $app['twig']->render('soundcloud-player.twig', array(
'url'=>$mix->getUrl(),
'player_type'=>'artwork',
'url'=>$mix->getUrl(),
'color'=>'ff0948',
'height'=>'200',
'width'=>'200'
)) . '<p class="delete-mix-wrapper"><a class="delete-mix" data-mix-id="'.$mix->getId().'" data-post-to="'.$app['url_generator']->generate('delete-mix-post').'" href="javascript:;">Delete</a></p>' . '</div>';
return new Response(json_encode(array(
'html'=>$result,
'status'=>'true'
)));
thats the code that creates the div dynamically.
yet when I click delete, nothing happens.
here comes the code that handles the post request through js.
$('a.delete-mix').on('click', function() {
var parent = $(this).closest('div.my-music-item');
$.post($(this).attr('data-post-to'), { mix_id: $(this).attr('data-mix-id') })
.done(function(data) {
parent.css('background-color', 'pink');
parent.fadeOut('fast');
});
});
I have been reading a lot, without much luck! also first my code looked a bit differently, it didnt had the
.on('click', function() {
but rather was
.click(function() {
Thanks in advance for your input!