0

I have a form the user put values and submit and an image of cross is also appended at the same time as a sibling. I want the image to when clicked remove itself and it's previous sibling. But it's not working

$newform.on('submit', function(e) {
    e.preventDefault();
    var text = $newform.find('input:text').val();
    var url = document.getElementById('addurl').value;
    $list.append('<li class="links"><a href="'+ url +'"><div class="spann">' + text +'</div></a></li>');
    $list.append('<img src="images/close.png" class="cross">');
});
$('.cross').on('click', function() {
    $(this).prev().remove();
    $(this).remove();
});
  • Post a [mcve] please – j08691 Sep 22 '21 at 16:19
  • Did you see what $(this) returns in the function? If you have your images wrapped with
    or
  • , the $(this).prev() may not return anything. Please post the html as well, so that its easier to provide an answer
– Akash Pinnaka Sep 22 '21 at 16:20
  • Also see https://stackoverflow.com/a/8111171/535480 – James Sep 22 '21 at 16:21
  • @Akash Pinnaka, no the image is not appended inside any element. It is appended naked . – Sahil Handique Sep 22 '21 at 19:06