I have very little knowledge of javascript and jquery, but I was able to put the following code together.
I want to add the result of 'name' to the div "displayName" on an html page when I click the "nameCreationButton" button on the html page. When I click "nameCreationButton", absolutely nothing happens.
I declared the variable 'name' outside of the function 'generator', so my jquery should be able to access it, right? I have spent hours looking at this, and I feel silly for not understanding why it will not work.
function generator(){
var firstName = ["John","Mike","Robert","Patrick"];
var lastName = ["Smith","Johnson","Williams","Anderson"];
var randomNumber1 = parseInt(Math.random() * firstName.length);
var randomNumber2 = parseInt(Math.random() * lastName.length);
var name = firstName[randomNumber1] + " " + lastName[randomNumber2];
}
var name;
generator();
$(document).ready(function(){
$(".nameCreationButton").click(function(){
$(".displayName").append('name');
});
});
Here is my HTML page if it is any use.
<!DOCTYPE html>
<html>
<head>
<link rel='stylesheet' href='style.css'/>
<script type= 'text/javascript' src='nameGenerator_JS.js'></script>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
<button class = "nameCreationButton">Click Here to Generate Name</button>
<div class = "displayName"></div>
</body>
</html>
'quote?class="nameCreationButton".text()