Franco Franco - 2 months ago 8
HTML Question

Add element to document and then remove it using Jquery

I am adding text to an element in Jquery using

$('.alert-saved').append('<br /><a href="/?similar=' + release_id + '&source=index">See more like this >></a>')


I then show this to the user and pause.

$('.alert-saved').delay(5000).fadeOut(2000);


I would now like to remove all the text I appended.

I have tried this but it didn't work

$('.alert-saved').remove('<br /><a href="/?similar=' + release_id + '&source=index">See more like this >></a>')

Answer

Just pass an empty HTML string argument:

$('.alert-saved').html('');

EDIT 1

If you need to keep other elements, you can use this method:

var newLine = jQuery('<br /><a href="#your whatever">See more like this</a>');
jQuery(".alert-saved").append(newLine);
setTimeout(function() {
  jQuery(newLine).remove();
}, 2000)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="alert-saved">
  <span>I wanna stay!</span>
</p>

Comments