js replace text in element code example
Example 1: javascript set text of div
document.getElementById("content").innerHTML = "whatever";
Example 2: javascript replace element
<html>
<head>
</head>
<body>
<div>
<a id="myAnchor" href="http://www.stackoverflow.com">StackOverflow</a>
</div>
<script type="text/JavaScript">
var myAnchor = document.getElementById("myAnchor");
var mySpan = document.createElement("span");
mySpan.innerHTML = "replaced anchor!";
myAnchor.parentNode.replaceChild(mySpan, myAnchor);
</script>
</body>
</html>
Example 3: javascript when button clicked different text appears
function show() {
var box = document.getElementById('box');
var displaySetting = myClock.style.display;
var showButton = document.getElementById('showButton');
if (displaySetting == 'block') {
myClock.style.display = 'none';
clockButton.innerHTML = 'Show box';
}
else {
myClock.style.display = 'block';
clockButton.innerHTML = 'Hide box';
}
}
Example 4: css change the button text value
document.getElementById("myButton").innerHTML="new button text";
Example 5: javascript replace element
var el = document.querySelector('div');
var newEl = document.createElement('p');
newEl.innerHTML = '<b>Hello World!</b>';
el.parentNode.replaceChild(newEl, el);