views:

145

answers:

5
function text_window()
{

var xmlDoc;
if (window.XMLHttpRequest)
  {
  xmlDoc=new window.XMLHttpRequest();
  xmlDoc.open("GET","cd_catalog.xml",false);
  xmlDoc.send("");
  xmlDoc=xmlDoc.responseXML;
  }
// IE 5 and IE 6
else if (ActiveXObject("Microsoft.XMLDOM"))
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async=false;
  xmlDoc.load("cd_catalog.xml");
  }

document.write("<textarea rows=\"10\" cols=\"90\" ID=\"Textarea1\" NAME=\"Textarea1\" readonly=\"readonly\">");

var x=xmlDoc.getElementsByTagName("CD");
for (i=0;i<x.length;i++)
  { 
  document.write(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);  

//How do I put a line break here?  

  document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
  }
document.write("</textarea>");
}

I would like to edit the scrolling textarea where I wrote the comment "//How do I put a line break here?"
Currently, it prints all on the same row/line.

Output:
Bob Dylan" "Empire BurlesqueBonnie TylerUKHide your heartDolly PartonUSAGreatest HitsGary MooreUKStill got the bluesEros.......

Desired Output:
Bob Dylan Empire
BurlesqueBonnie Tyler Hide your heart
Dolly Parton Greatest Hits
Gary Moore Still got the bluesEros.......
etc.

+2  A: 

try

document.write("\n")

in the place where you have that comment

mkoryak
What the ? I thought I tried that, guess not. Thanks.
Tommy
+1  A: 

Replace your comment with
document.write("\n");

Textarea content is based off of the formatting in the html document. When you do a document.write(), it doesn't put in any newlines by default, which makes the HTML one big long string.

JamesMLV
+1  A: 

A new line in a textarea is a \n (this is also true for javascript alerts and confirms)

document.write("\n");

Please note that document writes aren't exactly the best way to achieve what you're trying to do and that by answering I am not condoning it I'm just tying to solve your problem (this is for all the pedants out there - I know, I'm one too)

Darko Z
+1  A: 

use a \n for newlines, as in:

document.write("\n");
Gabriel Hurley
+1  A: 

Don't transfer data into a textarea like that, it'll fail for characters that are special in HTML. For example if Michael Jackson had produced an album called “</textarea><script>alert('POTATO!');</script>” you'd really be in trouble.

(I can't remember whether he actually did or not. Maybe that was Lionel Ritchie. POTATO!)

instead, it would be easier to write to the textarea's value directly. For example:

document.write('<textarea rows="10" cols="90" ID="Textarea1" NAME="Textarea1" readonly="readonly"></textarea>');
var area= document.getElementById('Textarea1');

for (i=0; i<x.length; i++) {
    var artist= x[i].getElementsByTagName("ARTIST")[0].firstChild.data;
    var title= x[i].getElementsByTagName("TITLE")[0].firstChild.data;
    area.value+= artist+'\n'+title+'\n'; 
}
bobince