How do I replace text inside a div element?

JavascriptHtmlDom

Javascript Problem Overview


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
}

Javascript Solutions


Solution 1 - Javascript

You can simply use:

fieldNameElement.innerHTML = "My new text!";



 

Solution 2 - Javascript

Updated for everyone reading this in 2013 and later:

This answer has a lot of SEO, but all the answers are severely out of date and depend on libraries to do things that all current browsers do out of the box.

To replace text inside a div element, use Node.textContent, which is provided in all current browsers.

fieldNameElement.textContent = "New text";

Solution 3 - Javascript


function showPanel(fieldName) {
var fieldNameElement = document.getElementById("field_name");
while(fieldNameElement.childNodes.length >= 1) {
fieldNameElement.removeChild(fieldNameElement.firstChild);
}
fieldNameElement.appendChild(fieldNameElement.ownerDocument.createTextNode(fieldName));
}

The advantages of doing it this way:

  1. It only uses the DOM, so the technique is portable to other languages, and doesn't rely on the non-standard innerHTML
  2. fieldName might contain HTML, which could be an attempted XSS attack. If we know it's just text, we should be creating a text node, instead of having the browser parse it for HTML

If I were going to use a javascript library, I'd use jQuery, and do this:


$("div#field_name").text(fieldName);

Note that @AnthonyWJones' comment is correct: "field_name" isn't a particularly descriptive id or variable name.

Solution 4 - Javascript

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");

Solution 5 - Javascript

$('field_name').innerHTML = 'Your text.';

One of the nifty features of Prototype is that $('field_name') does the same thing as document.getElementById('field_name'). Use it! :-)

John Topley's answer using Prototype's update function is another good solution.

Solution 6 - Javascript

The quick answer is to use innerHTML (or prototype's update method which pretty much the same thing). The problem with innerHTML is you need to escape the content being assigned. Depending on your targets you will need to do that with other code OR

in IE:-

document.getElementById("field_name").innerText = newText;

in FF:-

document.getElementById("field_name").textContent = newText;

(Actually of FF have the following present in by code)

HTMLElement.prototype.__defineGetter__("innerText", function () { return this.textContent; })
	
HTMLElement.prototype.__defineSetter__("innerText", function (inputText) { this.textContent = inputText; })

Now I can just use innerText if you need widest possible browser support then this is not a complete solution but neither is using innerHTML in the raw.

Solution 7 - Javascript

If you really want us to just continue where you left off, you could do:

if (fieldNameElement)
    fieldNameElement.innerHTML = 'some HTML';

Solution 8 - Javascript

nodeValue is also a standard DOM property you can use:

function showPanel(fieldName) {
  var fieldNameElement = document.getElementById(field_name);
  if(fieldNameElement.firstChild)
    fieldNameElement.firstChild.nodeValue = "New Text";
}

Solution 9 - Javascript

el.innerHTML='';
el.appendChild(document.createTextNode("yo"));

Solution 10 - Javascript

If you're inclined to start using a lot of JavaScript on your site, jQuery makes playing with the DOM extremely simple.

http://docs.jquery.com/Manipulation">http://docs.jquery.com/Manipulation</a>

Makes it as simple as: $("#field-name").text("Some new text.");

Solution 11 - Javascript

Use innerText if you can't assume structure

Solution 12 - Javascript

function showPanel(fieldName) {
  var fieldNameElement = document.getElementById(field_name);
  
  fieldNameElement.removeChild(fieldNameElement.firstChild);
  var newText = document.createTextNode("New Text");
  fieldNameElement.appendChild(newText);
}

Solution 13 - Javascript

Here's an easy jQuery way:

var el = $('#yourid .yourclass');

el.html(el.html().replace(/Old Text/ig, "New Text"));

Solution 14 - Javascript

In HTML put this

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

In Javascript put this

var fieldNameElement = document.getElementById('field_name');
		if (fieldNameElement)
		{fieldNameElement.innerHTML = 'some HTML';}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsblundyView Question on Stackoverflow
Solution 1 - Javascript17 of 26View Answer on Stackoverflow
Solution 2 - JavascriptmikemaccanaView Answer on Stackoverflow
Solution 3 - JavascriptDaniel PapasianView Answer on Stackoverflow
Solution 4 - JavascriptJohn TopleyView Answer on Stackoverflow
Solution 5 - JavascriptceejayozView Answer on Stackoverflow
Solution 6 - JavascriptAnthonyWJonesView Answer on Stackoverflow
Solution 7 - JavascriptMilan BabuškovView Answer on Stackoverflow
Solution 8 - JavascriptpalswimView Answer on Stackoverflow
Solution 9 - JavascriptAdrian AdkisonView Answer on Stackoverflow
Solution 10 - JavascriptSteve PerksView Answer on Stackoverflow
Solution 11 - JavascriptYouth overturnView Answer on Stackoverflow
Solution 12 - JavascripthollystylesView Answer on Stackoverflow
Solution 13 - JavascriptCosminView Answer on Stackoverflow
Solution 14 - JavascriptTreesView Answer on Stackoverflow