user3113376
user3113376

Reputation: 171

Writing to a div from javascript

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.

Upvotes: 0

Views: 72

Answers (4)

Chandra Prakash
Chandra Prakash

Reputation: 791

<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.

Upvotes: 0

Amila Iddamalgoda
Amila Iddamalgoda

Reputation: 4286

<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>

Upvotes: 0

deviloper
deviloper

Reputation: 7240

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>

Upvotes: 0

Raghu
Raghu

Reputation: 2563

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()

Upvotes: 1

Related Questions