legolas Posted December 3, 2007 Share Posted December 3, 2007 From Javascript I do a responseXML-read from a php-script that sends some rawtext that includes the string <br />.It sends in the form of: <keyword><![CDATA[" . $text . "]]></keyword>When it displays on the screen, it is untranslated, that is the <br /> is written, but I want it translated into a line break instead.How do I accomplish this? Any suggestions would be welcomed! Link to comment Share on other sites More sharing options...
justsomeguy Posted December 3, 2007 Share Posted December 3, 2007 How is the value being written to the page? Link to comment Share on other sites More sharing options...
legolas Posted December 3, 2007 Author Share Posted December 3, 2007 Using DOM I create av 'div' element, style it (absolute position, top, left, height and width) andthen I just append it with document.body.appendChild(mytext);and it displays-------------------------------Lorem ipsum dolor sit amet, consectetuer adipiscing elit.<br />Vestibulum pharetra pellentesque ligula.<br />Ut dui. -------------------------------but instead I want line breaks converted to read it as-------------------------------Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Vestibulum pharetra pellentesque ligula.Ut dui. ------------------------------- Link to comment Share on other sites More sharing options...
Ingolme Posted December 3, 2007 Share Posted December 3, 2007 You have to append each child individually. That's how DOM works: var A = new Array();A[0] = document.createTextNode("Lorem ipsum dolor sit amet, consectetuer adipiscing elit.");A[1] = document.createElement("br");A[2] = document.createTextNode("Vestibulum pharetra pellentesque ligula.");A[3] = document.createElement("br");A[4] = document.createTextNode("Ut dui.");var x;for (x=0;x<A.length;x++) {document.body.appendChild(A[x]);} Link to comment Share on other sites More sharing options...
justsomeguy Posted December 3, 2007 Share Posted December 3, 2007 To clarify, when you append a string to a node it creates a new text node and puts the string into the text node. Text nodes are literal, so if you assign "<br>" to a text node it writes that out as text, it does not get rendered as HTML. You can either create the individual <br> elements like Ingolme mentioned or you can use the innerHTML property to set the text instead of appendChild. Link to comment Share on other sites More sharing options...
legolas Posted December 3, 2007 Author Share Posted December 3, 2007 Thanks for the tip, but it can get complicated because I don't know if and how many <br /> I receive in the textblock. Link to comment Share on other sites More sharing options...
Ingolme Posted December 3, 2007 Share Posted December 3, 2007 I know, it's quite complicated. So the easy solution is to set it like this:document.body.innerHTML = A stringI don't really like it, because it's not standard, but sometimes there's no other option. Link to comment Share on other sites More sharing options...
legolas Posted December 3, 2007 Author Share Posted December 3, 2007 Ok, I was hoping for some Javascript function to fix this...It looks I have to check out how this innerHTML business works.Thanks for your time. Link to comment Share on other sites More sharing options...
justsomeguy Posted December 3, 2007 Share Posted December 3, 2007 You can replace br tags with newline characters if you want to. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.