Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove last append element jquery

Tags:

jquery

append

I have the following code:

$(this).children("a:eq(0)").append('<img src="'+ (arrowsvar.down[1])      +'" class="' + (arrowsvar.down[0])     + '" style="border:0;" />' ); 

Now I want to remove last appended element (an image). Please give suggestions.

like image 903
Basharat Ali Avatar asked Oct 30 '09 15:10

Basharat Ali


People also ask

How to remove append html in jQuery?

click(function() { $('. project_images'). remove(); return false; });

How to remove an element in jQuery?

To remove elements and content, there are mainly two jQuery methods: remove() - Removes the selected element (and its child elements) empty() - Removes the child elements from the selected element.

How to remove child element in jQuery?

In jQuery, in order to remove element and content you can use anyone of the following two jQuery methods: Methods: remove() – It is used to remove the selected element (and its child elements). empty() – It is used to removes the child elements from the selected element.


2 Answers

As an alternative, for those who likes more programmatic ways and syntax:

$('#container-element img').last().remove(); 
like image 163
Arthur Kushman Avatar answered Sep 22 '22 06:09

Arthur Kushman


You can use the :last-child selector to find the last appended element, then you can remove it:

$('img:last-child', this).remove(); // get the last img element of the childs of 'this' 
like image 30
Christian C. Salvadó Avatar answered Sep 22 '22 06:09

Christian C. Salvadó