Wanted to create random code, store it in variable and show it in html.
function GenerateButton() {
document.getElementById("btn1id").style.display = "none";
document.getElementById("Txt").style.display = "block";
document.getElementById("code").innerHTML = GFCode;
}
function randomString(length, chars) {
var result = '';
for (var i = length; i > 0; --i) result += chars[Math.round(Math.random() * (chars.length - 1))];
return result;
}
function codeFunc() {
document.write(randomString(4, '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'));
document.write("-");
document.write(randomString(4, '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'));
}
var GFCode = codeFunc;
<div class="btn0" id="btn1id" onclick="GenerateButton()">Generate</div>
<div class="Txt" id="Txt" style="display=none;"> The Code is: <span id="code"></span></div>
If i do the document.write in the html document the code is generating, but i can't get it working from the js document.