How to dynamically change content of a div using JavaScript?
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.
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.
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.
This should do it:
<div id="foo">Div you want to change</div>
And in JavaScript:
document.getElementById('foo').innerHTML = 'Your 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