Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set outerHTML with jQuery

I have a UserControl. Ex:

<div id="divItem"> some html </div> 

The ajax request return new html of this UC from server. Ex:

<div id="divItem">     new html </div> 

I want to replace the old html by the new one. How could I do that. Thanks.

like image 530
Jin Ho Avatar asked Nov 24 '11 09:11

Jin Ho


People also ask

What is outerHTML in jQuery?

The outerHTML is often used to replace the element and its contents completely. It differs from the innerHTML as innerHTML only represent the HTML of contents of an element, while outerHTML includes the HTML of element itself with its descendants.

How do I set inner text in jQuery?

jQuery text() Method Tip: To set or return the innerHTML (text + HTML markup) of the selected elements, use the html() method.

How do I get the whole HTML in jQuery?

To get HTML content of an element using jQuery, use the html() method. The html() method gets the html contents of the first matched element.

How do I get the HTML inside a div using jQuery?

With jQuery, you can use the . append() method to insert the specified HTML as the last child of the div container.


1 Answers

If you also return the div divItem

$("#divItem").replaceWith("NEW HTML"); 

Put the new HTML on the spot or replace the innerHTML, since they got the same container:

$("#divItem").html($("NEW HTML").html()); 

If you dont return the div divItem

Just put the new html:

$("#divItem").html("NEW HTML"); 
like image 118
Niels Avatar answered Sep 19 '22 15:09

Niels