How do I replace text inside a div element?

sblundy picture sblundy · Sep 23, 2008 · Viewed 534.7k times · Source

I need to set the text within a DIV element dynamically. What is the best, browser safe approach? I have prototypejs and scriptaculous available.

<div id="panel">
  <div id="field_name">TEXT GOES HERE</div>
</div>

Here's what the function will look like:

function showPanel(fieldName) {
  var fieldNameElement = document.getElementById('field_name');
  //Make replacement here
}

Answer

17 of 26 picture 17 of 26 · Sep 23, 2008

You can simply use:

fieldNameElement.innerHTML = "My new text!";