Use the textContent property to change the text of a div element, e.g. div. textContent = 'Replacement text' . The textContent property will set the text of the div to the provided string, replacing any of the existing content.
div elements don't have a . value property that would get submitted to the backend; use an input element for that.
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.
Assuming you're not using jQuery or some other library that makes this sort of thing easier for you, you can just use the element's innerHTML property.
document.getElementById("content").innerHTML = "whatever";
$('#content').html('whatever');
Get the id of the div
whose content you want to change then assign the text as below:
var myDiv = document.getElementById("divId");
myDiv.innerHTML = "Content To Show";
you can use following helper function:
function content(divSelector, value) {
document.querySelector(divSelector).innerHTML = value;
}
content('#content',"whatever");
Where #content
must be valid CSS selector
Here is working example.
Additionaly - today (2018.07.01) I made speed comparison for jquery and pure js solutions ( MacOs High Sierra 10.13.3 on Chrome 67.0.3396.99 (64-bit), Safari 11.0.3 (13604.5.6), Firefox 59.0.2 (64-bit) ):
document.getElementById("content").innerHTML = "whatever"; // pure JS
$('#content').html('whatever'); // jQuery
The jquery solution was slower than pure js solution: 69% on firefox, 61% on safari, 56% on chrome. The fastest browser for pure js was firefox with 560M operations per second, the second was safari 426M, and slowest was chrome 122M.
So the winners are pure js and firefox (3x faster than chrome!)
You can test it in your machine: https://jsperf.com/js-jquery-html-content-change
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