May 22 2008

Using javascript to set InnerText in Firefox and IE

Internet Explorer has a nice property .innerText which sets non-HTML contents inside an HTML container. Firefox does not have this function. I found a solution that looks like this:

function UpdatePreview(sourceControl, destinationContainer)
{
    if (destinationContainer.firstChild)
    {
        // if we have a child item, overwrite it
        destinationContainer.firstChild.nodeValue = 
sourceControl.value; } else { //create a new text child item destinationContainer.appendChild(document.createTextNode(
sourceControl.value));
}
}

UPDATE: Of course you can always use the lovely jQuery library to do this and much, much, much, more!.

Comments (View)
blog comments powered by Disqus

Please...

Leave a comment if this has helped or offended you.

StackOverflow Id