Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change content of a div using JavaScript

How to dynamically change content of a div using JavaScript?

like image 897
Rikard Avatar asked Feb 02 '10 17:02

Rikard


People also ask

How do I edit text inside a div?

Answer: Use the HTML5 contenteditable Attribute You can set the HTML5 contenteditable attribute with the value true (i.e. contentEditable="true" ) to make an element editable in HTML, such as <div> or <p> 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.

Can JS change HTML content?

Yes, it is possible to change the content of the HTML in javascript. Usually HTML content will be in HTML tags such as <p>, <span>, etc. In javascript, we have DOM methods that have an ability to access the HTML tags. Those methods, such as document.


1 Answers

This should do it:

<div id="foo">Div you want to change</div>

And in JavaScript:

document.getElementById('foo').innerHTML = 'Your content';
like image 108
Tatu Ulmanen Avatar answered Sep 20 '22 12:09

Tatu Ulmanen