...

View Full Version : How do I format text



ingie001
12-12-2006, 08:39 PM
I want to display either "text 1" or "text 2" after a button has been pressed.
The text is long (maybe 300 words). I've used the following to generate it.

var myDiv = document.getElementById("displayText");

myDiv.appendChild(document.createTextNode("This is the text part 1"));
myDiv.appendChild(document.createTextNode("This is the text part 2"));

The trouble is it just displays the 2 texts strung together. I cant get any newlines or different fonts etc. Can anyone give advice please

feras_wilson
12-12-2006, 08:54 PM
You mean like this?: ||
v


<p>text text text text. text text text. txet text text.

You dont want "txet" to be viewed?

ingie001
12-12-2006, 09:05 PM
The text is in the following commands
myDiv.appendChild(document.createTextNode("This is the text part 1"));
myDiv.appendChild(document.createTextNode("This is the text part 2"));

You can not put any markup in (e.g. "<p>This is the text part 1<br>" as it ignores the <p> <br> and just treats it as text when it is displayed by the HTML - <div id="displayText"></div>

liorean
12-12-2006, 09:07 PM
DOM text nodes are considered preparsed. This means that if you want a newline, you can add a newline using the JavaScript string escape mechanism, '\n'. Another solution is to insert a br element.


If you want to change fonts or other style you need to create an element which has a style (or which has a class that will be styled from CSS present in the document already) and add the text you want to give that style as a child node to that element.

ingie001
12-12-2006, 09:19 PM
I am not an experienced javascript programmer but have tried to put the \n in the text as follows
myDiv.appendChild(document.createTextNode("This is the text part 1 \n"));
myDiv.appendChild(document.createTextNode("This is the text part 2"));

this did not have any effect and produced the following line
This is the text part 1 This is the text part 2

It did not display the \n which I expected it would do. If I had put a <p> in the text it would have just displayed it

ingie001
12-12-2006, 09:47 PM
I am not an experienced javascript programmer but have tried to put the \n in the text as follows
myDiv.appendChild(document.createTextNode("This is the text part 1 \n"));
myDiv.appendChild(document.createTextNode("This is the text part 2"));

this did not have any effect and produced the following line
This is the text part 1 This is the text part 2

It did not display the \n which I expected it would do. If I had put a <p> in the text it would have just displayed it
I am not an experienced javascript programmer but have tried to put the \n in the text as follows
myDiv.appendChild(document.createTextNode("This is the text part 1 \n"));
myDiv.appendChild(document.createTextNode("This is the text part 2"));

this did not have any effect and produced the following line
This is the text part 1 This is the text part 2

It did not display the \n which I expected it would do. If I had put a <p> in the text it would have just displayed it

david_kw
12-12-2006, 11:42 PM
You do as liorean said and create a <br> node between the two like this:

myDiv.appendChild(document.createTextNode("This is the text part 1"));
myDiv.appendChild(document.createElement("br"));
myDiv.appendChild(document.createTextNode("This is the text part 2"));

david_kw

ingie001
12-13-2006, 10:57 AM
Thanks David_KW - I needed it spelling out as you have done.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum