Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replacing div content with javascript

Tags:

javascript

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> 
like image 948
sameold Avatar asked Aug 24 '11 10:08

sameold


People also ask

How do I change the content of a 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.

Is used to replace elements content in JavaScript?

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.

How do I change the inner text of a div?

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.

Can you use div in JavaScript?

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.


1 Answers

document.getElementById("test").innerHTML = "new content" 
like image 167
Ian R.B. Avatar answered Sep 19 '22 05:09

Ian R.B.