1

I have a simple Js function that generates a list of random numbers based on how many the user wants. The function works fine, and logs fine, but it isn't displaying like I'd like it to. I'm new to Javascript, so I tried using the \n escape character, but it didn't do anything. Any help would be appreciated.

function generateIDs() 
{

var num = document.getElementById('numberToGenerate').value;
var par = document.getElementById('numbers');
var button = document.getElementById('genButton');
button.disabled = true;

for (var x=0;x<num;x++) 
{

var id = Math.floor((Math.random()*10000)+1);
par.innerHTML = id;


}


    <form>
Auto-Generate <input type="text" name="number" id="numberToGenerate"/> IDs.
<button type="button" onclick="generateIDs()" id="genButton">Go!</button>
</form>

<p id="numbers">

</p>

1 Answer 1

2

\n doesn't mean much to a browser; use <br/> instead.

Example:

// snip
for (var x=0;x<num;x++) 
{
    var id = Math.floor((Math.random()*10000)+1);
    par.innerHTML = id.toString() + '<br/>';       
}
//snip

Note that this is going to overwrite the previous value on each iteration. You probably want this:

par.innerHTML += id.toString() + '<br/>';

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

1 Comment

how would you reset this, to prevent printing again without clearing the field.

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.