This post will discuss how to replace the content of a div element in JavaScript and jQuery.

1. Using JavaScript

The most common approach to replace the content inside an element is with the innerHTML property. Alternatively, you can use the textContent to set the element’s text content, which is considered safe against XSS attacks.

JS


HTML



Edit in JSFiddle

2. Using jQuery

With jQuery, you can use the .html() to replace the contents of any element which uses the browser’s innerHTML property. You can also use the .text() method.

jQuery


HTML



Edit in JSFiddle

3. Using Prototype.js

If you’re using the Prototype.js library, consider using the update() method.

Prototype.js


HTML



Edit in JSFiddle

That’s all about replacing the content of a div element in JavaScript and jQuery.