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() {
    // get the box
    var box = document.getElementById('box');

    // get the current value of the box's display property
    var displaySetting = myClock.style.display;

    // also get the box button, so we can change what it says
    var showButton = document.getElementById('showButton');

    // now toggle the box and the button text, depending on current state
    if (displaySetting == 'block') {
      // box is visible. hide it
      myClock.style.display = 'none';
      // change button text
      clockButton.innerHTML = 'Show box';
    }
    else {
      // box is hidden. show it
      myClock.style.display = 'block';
      // change button text
      clockButton.innerHTML = 'Hide box';
    }
  }

Example 4: css change the button text value

document.getElementById("myButton").innerHTML="new button text";

Example 5: javascript replace element

// select the element that will be replaced
	var el = document.querySelector('div');
	
	// <a href="/javascript/manipulation/creating-a-dom-element-51/">create a new element</a> that will take the place of "el"
	var newEl = document.createElement('p');
	newEl.innerHTML = '<b>Hello World!</b>';
	
	// replace el with newEL
	el.parentNode.replaceChild(newEl, el);