View Full Version : XML in textarea

01-21-2011, 11:34 PM
Hi all,

I'm loading external xml and displaying specific tagged elements in a textarea.
It works fine in IE because I can turn the variable object into text with a simple object.text property, but in Firefox (using the serializeToString method), the object appears in my textarea tags and all.

The code looks something like this (this is just a dummy script - imagine the xml is already loaded into the object xmlDoc):

function writePage(){
document.write('<textarea name="inputText">'+myText+'</textarea>');

function xmlIsLoaded(){
if(window.ActiveXObject){ //tests for IE
myText = xmlDoc.getElementsByTagName("desiredTag")[0].text;
} else { //tests for FF
myText = (new XMLSerializer()).serializeToString(xmlDoc.getElementsByTagName("desiredTag")[0]);

What am I missing here? I've tried toString, textContent, innerText, childNodes - just about everything I can think of to get just the text w/o the tags from the xml for the FF version.

Any thoughts?


rnd me
01-22-2011, 01:28 AM
myText = xmlDoc.getElementsByTagName("desiredTag")[0].textContent;

01-25-2011, 06:57 PM
Heh. How embarrassingly simple. I could have sworn I tried that, but clearly I hadn't.

Just to clarify for posterity's sake: my error was in using the textContent property with the xmlSerializer.

Thanks rnd me!

rnd me
01-25-2011, 08:57 PM
one way to half the amount of code you write is to use a string property name constant and array notation on you dom path.

put this in an onload() or at the top of first script you use:

TEXT= "v"!="\v" ? "textContent" : "text" ;

now, you can use the same line of code for both ie and everyone else:

myText = xmlDoc.getElementsByTagName("desiredTag")[0][TEXT];

it's a lot better than dozens of IFs cluttering your code...