I have this function that generates a random string!
function randString() {
var char = "0123456789abcdefghijklmnopqrstuvwxyz",
fullchar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
genHash = "",
i;
for (i = 0; i < 8; i++) {
var rnum = Math.floor(Math.random() * char.length)
return genHash += char.substring(rnum, rnum + 1)
}
}
What I'm trying to do is when I click a button I want my genHash to be various class names.
$("[data-action=newlist]").on("click", function() {
randString()
var newBtnList = $("<em>", {
text: genHash,
title: genHash,
class: "new-"+ genHash
}).appendTo("[data-action=list-tree]").on("click", function() {
// Show active editor
$("." + genHash).removeClass("hide")
})
})
I know I can do it inside the randString() function (such as set it as an input's value and grab the value back from the input), but what I'm trying to figure out is how can I to it outside of the function, thus return the string from the function as states in my title.
function randString() {
var char = "0123456789abcdefghijklmnopqrstuvwxyz",
fullchar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
genHash = "",
i;
for (i = 0; i < 8; i++) {
var rnum = Math.floor(Math.random() * char.length)
return genHash += char.substring(rnum, rnum + 1)
}
}
// Create a new list
$("[data-action=newlist]").on("click", function() {
randString()
var newBtnList = $("<em>", {
text: genHash,
title: genHash,
class: "new-"+ genHash
}).appendTo("[data-action=list-tree]").on("click", function() {
// Show active editor
$("." + genHash).removeClass("hide")
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" data-action="newlist">
New
</button>
<div data-action="list-tree"></div>