insert/remove HTML content between div tags

A simple way to do this with vanilla JavaScript would be to use appendChild.

var mydiv = document.getElementById("mydiv");
var mycontent = document.createElement("p");
mycontent.appendChild(document.createTextNode("This is a paragraph"));
mydiv.appendChild(mycontent);

Or you can use innerHTML as others have mentioned.

Or if you would like to use jQuery, the above example could be written as:

$("#mydiv").append("<p>This is a paragraph</p>");

If you're replacing the contents of the div and have the HTML as a string you can use the following:

document.getElementById('mydiv').innerHTML = '<span class="prego">Something</span>';

// Build it using this variable
var content = "<span class='prego'>....content....</span>"; 
// Insert using this:
document.getElementById('mydiv').innerHTML = content;

Tags:

Javascript

Dom