<!DOCTYPE html>
<html>
<head>
<title>Random Name</title>
</head>
<body>
<div id="container">
<p id="name"></p>
</div>
<script>
var randomName = function() {
var arry = ["Kevin", "John", "Mabel", "Lucy", "Isabella", "Ryan", "Clyde"];
var random = arry[Math.round(Math.random() * arry.length)];
return random;
};
// create button
var button = document.createElement("input");
var id = "btn";
button.type = "submit";
button.value = "click me!";
button.setAttribute("id", "btn");
document.body.appendChild(button);
window.onload = function() {
document.getElementById("btn").addEventListener("click", function(e) {
document.getElementById("name").innerHTML = randomName();
console.log("hi");
}
);
};
</script>
</body>
</html>
The code above is rendered as follows: