document.write and .innerHTML | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

document.write and .innerHTML

I have a piece of code.... Here is a small part of it: //(When they press the button, this code will start) var out = document.getElementById("ans"); for (var i = 0; i < input; i++) { var num = parseInt(Math.random() * 100); if (num > 78){ var output = char[0]; }else{ var output = char[num]; } out.innerHTML = output; //document.write(output); //You dont need to worry too much about the top code, just the output Ok, now this will produce lets say... 8 characters (S45#...) Now whenever I say document.write, it will give me all the characters, but on a new page... so i decided to make a <p></p> on the html file and give it an id called "ans"... Now i tried using out.innerHTML = output; to try and get it to paste in that paragraph, but it only gives me 1 character instead of 8... How do i get it to paste all 8 characters there?

15th Jan 2019, 5:33 PM
Stephen Van Der Westhuizen
Stephen Van Der Westhuizen - avatar
2 Answers
+ 2
Another way to think about this: If I have a string and I want to append to it, what operation should I use? var s="Existing string content"; s = ...
15th Jan 2019, 5:38 PM
Kirk Schafer
Kirk Schafer - avatar
0
It's only appending one character because it only contains one character 🤔 Psst... Checkout my password generator for some tips.
15th Jan 2019, 6:41 PM
Valtrius Malleus
Valtrius Malleus - avatar