The code below create 10 random strings, the idea is to have each string in a 'li'. With what I have, I able to create 10 random 'li', but after the sencond 'li' its a new string but with previous one. Is there a way to fix this?
Ex: This is what I trying to get
String 1
String 2
String 3
.......
The output that im currently getting is
String 1
String 1String 2
String 1String 2String 3
.....
Demo: https://jsfiddle.net/73cmb11q/
function names()
{
var txt = "abcdefghiklmnopqrstuvwxyz";
var name_length = 8;
var randomName = '';
var randomLength = Math.trunc(Math.random() * (8 - 4) + 4);
console.log(randomLength);
for(var j=0; j<10; j++)
{
var randomLength = Math.trunc(Math.random() * (8 - 4) + 4);
for(var i=0; i<randomLength; i++)
{
var rname = Math.floor(Math.random() * txt.length);
randomName += txt.substring(rname, rname+1);
}
var divName = document.getElementById('names');
divName.innerHTML += '<li>' + randomName + '</li>';
}
}
<form name="randomform">
<input type="button" value="Create" onClick="names()">
<input type="text" name="randomfield">
</form>
<ul id="names">
</ul>
randomNamevariable declaration inside the first for loop should fix it.