(I am new to programming)
The program: I have made a relatively simple JavaScript program, that generates a password when you click on a button.
The problem: Displaying the password with document.write
, the whole page is overwritten, meaning that the button is removed and you have no opportunity to generate a new password. Therefore, I would like the password to go inside the paragraph with Id "pw".
Note that document.write is disabled in JSFiddle, so the result is set to display in console. JSFiddle: https://jsfiddle.net/3qh01ctp/1/
Thank you very much in advance, any help/advice appreciated!
var arr = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
function generate () {
for (i = 0; i < 10; i++) {
randomLetter = arr[Math.floor(arr.length * Math.random())];
console.log(randomLetter);
}
}
<!DOCTYPE html>
<html>
<head>
<button onclick="generate();">Generate password</button>
</head>
<body>
<p id="pw">
<!-- password should go here -->
</p>
</body>
</html>