Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opposite of append in jQuery

I use .append to add to a div

$(this).append('<ul><li>test</li></ul>'); 

how can I search for a <ul> and remove it if it exists in the children of $(this)?

like image 291
sami Avatar asked Feb 08 '11 14:02

sami


People also ask

What is the opposite function of append?

Show activity on this post. The opposite of . append() is . prepend() .

What is append and prepend?

prepend() .append() puts data inside an element at the last index; while. . prepend() puts the prepending element at the first index.

How remove and append in jQuery?

jQuery uses: . append(); and . remove(); functions to accomplish this task. We could use these methods to append string or any other html or XML element and also remove string and other html or XML elements from the document.

What is the alternative for append in Javascript?

replaceChildren() is a convenient alternative to innerHTML and append() append() appends nodes to the parent node. The contents that were inside the node before the append() invocation remain preserved.


2 Answers

You could use remove(). More information on jQuery remove().

$(this).children("ul").remove(); 

Note that this will remove all ul elements that are children.

like image 159
Josh K Avatar answered Oct 05 '22 15:10

Josh K


The opposite of .append() is .prepend().

From the jQuery documentation for prepend…

The .prepend() method inserts the specified content as the first child of each element in the jQuery collection (To insert it as the last child, use .append()).

I realize this doesn’t answer the OP’s specific case. But it does answer the question heading. :) And it’s the first hit on Google for “jquery opposite append”.

like image 43
elbowlobstercowstand Avatar answered Oct 05 '22 14:10

elbowlobstercowstand