post icon

Random quote generator using javascript

Here’s a simple way to generate random quotes on you webpage. Like the one I’m using on the right side of my Home page under the navigation.

//first create a new array
//here it's called pullQuote
//you can specify how ever many values you like ie: 5 here
var pullQuote = new Array(5);
//use the random generator to pick the array values randomly
var randomNumber = Math.floor(Math.random() * 5);
//write out the values you want in the array
pullQuote[ 0] =  "I like Internet Scripting, but it is very hard and sometimes makes my brain hurt!";
pullQuote[ 1] =  "Internet Scripting is the best course in the DMT program!";
pullQuote[ 2] =  "Learning JavaScript will help me become a better web designer!";
pullQuote[ 3] =  "I think JavaScript is for the monkeys!";
pullQuote[ 4] =  "JavaScript makes me go loopy!";
//create a new variable called quote
//make quote equal to the id of the element you want to put your values in
var quote = document.getElementById('quote');
//specify the variable and write into it using innerHTML
//use the pullQuote array and specify the array key to be random
quote.innerHTML = (pullQuote[randomNumber]);

That’s all! Pretty simple stuff! If you don’t have a clue what any of this means you can pretty much copy and paste this code. Just create and element on your page that has an id=”quote”. You also have to make sure those script tags get placed back in there properly. Here’s the js file.

No comments yet.

Leave a comment