1
function calcPrimesLoop() {
var primes = document.getElementById('primes');
primes.appendChild(document.createTextNode(" , /n , "+this.prime.nextPrime()));
calcPrimesDelay = setTimeout('calcPrimesLoop()', this.delay);
}

Okay so this is my code I am displaying an array of prime numbers. The issue is that I want each prime number to be on a seperate line but I am unable to do this. I have tried
/n and   but they have not worked. It is being displayed in a textarea in html. Thank you

0

2 Answers 2

3

You could append a br element:

primes.appendChild(document.createElement('br'));

...although usually the way you'd want to do this would be to put the primes in some kind of element container that you could style appropriately with CSS. A series of divs would automatically stack vertically:

var div = document.createElement('div');
div.appendChild(document.createTextNode(/*...your prime...*/));
primes.appendChild(div);

Two side notes on this line:

calcPrimesDelay = setTimeout('calcPrimesLoop()', this.delay);

First, it's almost always best to use function references, not strings, with setTimeout. So:

calcPrimesDelay = setTimeout(calcPrimesLoop, this.delay);

Second, unless you're declaring calcPrimesDelay somewhere you haven't shown, you're falling prey to The Horror of Implicit Globals.

Sign up to request clarification or add additional context in comments.

1 Comment

My only problem was it is meant to be \n not /n. I appreciate that divs are probably more practical but the line was for aseptic appeal. Thank you tho!
2

You should use a backslash instead of a forward slash (\n)

EDIT: The below only applies to "normal" elements. For a textarea, you should be doing primes.value += " , \n , "+this.prime.nextPrime();

Additionally, newlines are collapsed in HTML (if you write text on multiple lines in your source code, it comes out on one line) but you can "fix" this using simple CSS:

primes.style.whiteSpace = "pre-wrap";

Spread the word about white-space! People need to stop using <br /> tags just to get a newline!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.