Reputation: 3
I'm attempting to create a very beginner script that would choose a random word from a list of topics, in order to do this I've created an html page that I believe correctly embeds the javascript that I've mainly copied off of stackoverflow , but something isn't working correctly. My html button that should be generating a random word will do nothing. Below is html code, as well as the javascript code. I'm very new at this, please feel free to inform me on how to better word these questions, or of anything I've done wrong. Thanks!
<!DOCTYPE html>
<html>
<head>
<title>isportal</title>
<link rel="stylesheet" type="text/css" href
</head>
<body>
<h1>Completely Neccessary Generator </h1>
<div id="quoteDisplay">
<!--Quotes will display here -->
</div>
<button onclick="newQuote()">New Quote</button>
<script type="text/javascript" src="randomItem.js"></script>
</body>
</html>
Javascript code is
var myArray = [
"Administrative",
"Fiber Optic",
"BIOS",
"Security",
"Firewall",
"Mainframe",
"Power-cycling",
"HTML",
"Server-side",
"Coding-based",
"Power-orientied",
"Server",
"Connection-based",
"Networking"
];
function newQuote() {
var randomItem = myArray[Math.floor(Math.random()*myArray.length)];
Random.body.innerHTML = randomItem;
}
Expected results is the javascript will generate a random word based off of the array, actual results is that nothing is generated.
Upvotes: 0
Views: 38
Reputation: 329
Instead of
Random.body.innerHTML = randomItem;
Try
document.getElementById('quoteDisplay').innerHTML = randomItem;
This code will fetch the div with ID quoteDisplay
and set its HTML to that of randomItem
.
In the code that you have provided Random
is not defined and hence your code does not work. If you check the console, you should be getting a console error.
Upvotes: 1
Reputation: 553
I'm assuming you would like the random item to show in the div with id of 'quoteDisplay'
function newQuote() {
var randomItem = myArray[Math.floor(Math.random()*myArray.length)];
document.getElementById('quoteDisplay').innerHTML = randomItem;
}
Upvotes: 0