Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append and remove an <img > to <div> with jQuery?

Tags:

jquery

Original:

<div id="test">
...
</div>

After appending:

<div id="test">
...
<img src=".." />
</div>

After removing:

<div id="test">
...
</div>
like image 486
Misier Avatar asked Oct 05 '09 19:10

Misier


People also ask

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.

How can I remove image from preview as well as file tag jQuery?

To remove an image bind the click event on the class='delete' . On click select the image source and display confirmation alert. If OK button gets clicked then send an AJAX request where pass the path with a request: 2 . If response is 1 on successful callback then remove the <div > container.

How can you remove and HTML element using 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 do you delete something in jQuery?

jQuery remove() Method The remove() method removes the selected elements, including all text and child nodes. This method also removes data and events of the selected elements. Tip: To remove the elements without removing data and events, use the detach() method instead.


2 Answers

// add
$("#test").append("<img src='...' />")

// remove
$("#test img:last-child").remove()
like image 129
cpjolicoeur Avatar answered Oct 05 '22 03:10

cpjolicoeur


$('div#test').append('<img src=".."/>');
$('div#test > img').remove();

Note that you can give the image a class as well, in case you only want to remove the one you added.

like image 26
Samantha Branham Avatar answered Oct 05 '22 01:10

Samantha Branham