0

I'm trying to make a div box where it displays a randomized quote generated in the javascript, but it does not display when I run the code.

My html is simply

<div id="quotebox"></div>

my javascript is

var quotes = new Array()
quotes[0]="Simplicity is the ultimate sopistication";
quotes[1]="While I thought that I was learning how to live, I have been learning how to die.";
quotes[2]="The greatest deception men suffer is from their own opinions.";
quotes[3]="Art is never finished, only abandoned";
quotes[4]="Iron rusts from disuse; water loses its purity from stagnation. Even so does inaction sap the vigor of the mind.";

var randQuote=Math.floor(Math.random()*(quotes.length));
function showQuote(){
    document.getElementById("quotebox").innerHTML = quotes[randQuote];
}

Am I just making a silly mistake or what is going on? I've tried several different things now and it still doesn't display to the div.

2
  • 4
    Are you calling showQuote() anywhere? Or try checking your quotes variable to make sure it has the data in it. Commented Mar 12, 2014 at 4:05
  • I've tried calling it both in the <body> and <div> but nothing happens. What do you mean check the quotes variable? Commented Mar 12, 2014 at 4:14

4 Answers 4

1

You are not calling showQuote(); Here is the working JS fiddle http://jsfiddle.net/9bmAN/

var quotes = new Array()
quotes[0]="Simplicity is the ultimate sopistication";
quotes[1]="While I thought that I was learning how to live, I have been learning how to die.";
quotes[2]="The greatest deception men suffer is from their own opinions.";
quotes[3]="Art is never finished, only abandoned";
quotes[4]="Iron rusts from disuse; water loses its purity from stagnation. Even so does inaction sap the vigor of the mind.";

var randQuote=Math.floor(Math.random()*(quotes.length));
function showQuote(){
    document.getElementById("quotebox").innerHTML = quotes[randQuote];
}

showQuote()
Sign up to request clarification or add additional context in comments.

2 Comments

what is this comment for?
i deleted it..got mistakenly posted here. Thanks for notifying
0

Make sure you have an element (div, td, whatever) with the id: "quotebox":

function showQuote(){
   var quotes = new Array()
quotes[0]="Simplicity is the ultimate sopistication";
quotes[1]="While I thought that I was learning how to live, I have been learning how to die.";
quotes[2]="The greatest deception men suffer is from their own opinions.";
quotes[3]="Art is never finished, only abandoned";
quotes[4]="Iron rusts from disuse; water loses its purity from stagnation. Even so does inaction sap the vigor of the mind.";

var randQuote=Math.floor(Math.random()*(quotes.length));
    document.getElementById("quotebox").innerHTML = quotes[randQuote];
}
</script>
<body onload="showQuote();">
<div id="quotebox">

</div>
</body>

Comments

0
<script>
window.onload=function(){   
    var quotes = new Array()
    quotes[0]="Simplicity is the ultimate sopistication";
    quotes[1]="While I thought that I was learning how to live, I have been learning how to die.";
    quotes[2]="The greatest deception men suffer is from their own opinions.";
    quotes[3]="Art is never finished, only abandoned";
    quotes[4]="Iron rusts from disuse; water loses its purity from stagnation. Even so does inaction sap the vigor of the mind.";

    var randQuote=Math.floor(Math.random()*(quotes.length));
        document.getElementById("quotebox").innerHTML = quotes[randQuote];

};     
  </script>

<body >
<div id="quotebox"></div>
</body>

2 Comments

Nothing is still being displayed in the div.
Above code works fine :) Check the update! use window.onload()
0
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<script>
var quotes = new Array()
quotes[0]="Simplicity is the ultimate sopistication";
quotes[1]="While I thought that I was learning how to live, I have been learning how to die.";
quotes[2]="The greatest deception men suffer is from their own opinions.";
quotes[3]="Art is never finished, only abandoned";
quotes[4]="Iron rusts from disuse; water loses its purity from stagnation. Even so does inaction sap the vigor of the mind.";

var randQuote=Math.floor(Math.random()*(quotes.length));
function showQuote(){
    document.getElementById("quotebox").innerHTML = quotes[randQuote];
}
</script>
<body onload="showQuote()">
    <div id="quotebox"></div>
</body>
</html>

This code runs well on my browser. May be some problem with your browser.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.