sblundy sblundy - 4 months ago 11
HTML Question

How do I replace text inside a div element?

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

I would use Prototype's update method which supports plain text, an HTML snippet or any JavaScript object that defines a toString method.

$("field_name").update("New text");