I'm trying to do this with pure javascript, not with jquery. I have a div that has an id test
and contains other divs inside it. How do I empty the content of this div and replace it with other html?
<div id="test"> <div>...</div> <div>...</div> <div>...</div> </div>
Answer: Use the jQuery html() Method You can simply use the jQuery html() method to replace innerHTML of a div or any other element.
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.
Set the innerHTML Property of an Element querySelector('div') div. innerHTML = "My new text!"; to select the div with querySelector . And then we can set the innerHTML to a new string.
Using JavaScript In vanilla JavaScript, you can use the native createElement() method to create an HTML <div> element and the appendChild() method to append the <div> element to another container.
document.getElementById("test").innerHTML = "new content"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With