Click to See Complete Forum and Search --> : Arrays + bgColor = MOODY COMPUTER
tomyknoker
06-28-2003, 05:17 AM
Hi All,
I have this code below but I can't seem to get the bgcolor to change...anyone outthere see what I am doing wrong? I also want the mood to be written in the window as well as alert??
thanks again,
tom
<html>
<head>
<script>
var mood = new Array();
mood[1]= new Array("I am tired","blue");
mood[2]= new Array("I am happy","yellow");
mood[3]= new Array("I am cold","cyan");
mood[4]= new Array("I am hot","maroon");
mood[5]= new Array("I am angry","red");
mood[6]= new Array("I am sad","orange");
totm = (Math.floor(Math.random()*6)+1);
document.bgColor = mood[totm][1];
alert("Today " + mood[totm][0]);
</script>
</head>
<body>
</body>
</html>
SlankenOgen
06-28-2003, 06:07 AM
I'm using IE6 on Windows XP and it works fine.
Charles
06-28-2003, 06:07 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<script type="text/javascript">
<!--
Array.prototype.random = function () {return this[Math.round (Math.random() * Math.ceil (this.length / 10) * 10) % this.length]}
var mood = [{mood:"I'm tired", color:"blue"}, {mood:"I am happy", color:"yellow"}, {mood:"I am cold", color:"cyan"}, {mood:"I am hot", color:"maroon"}, {mood:"I am angry", color:"red"}, {mood:"I am sad", color:"orange"}].random();
alert (mood.mood);
document.bgColor = mood.color;
// -->
</script>
tomyknoker
06-28-2003, 07:28 AM
yeah I am using IE5 on a MAC and it doesn't work but tried it on the PC and your right it works! how would I change this code slightly so each time the page loads an input box is on the page and the user types "how do you feel today" and clicks submit...and then the function runs and also as well as the alert writes on the page how it feels??
thanks,
tom
Charles
06-28-2003, 07:41 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
<!--
Array.prototype.random = function () {return this[Math.round (Math.random() * Math.ceil (this.length / 10) * 10) % this.length]}
var mood = [{mood:"I'm tired", color:"blue"}, {mood:"I am happy", color:"yellow"}, {mood:"I am cold", color:"cyan"}, {mood:"I am hot", color:"maroon"}, {mood:"I am angry", color:"red"}, {mood:"I am sad", color:"orange"}].random();
document.write('<input type="text" id="question"><br><button id="ask">Submit</button>');
document.write('<p id="answer"> </p>');
document.getElementById('ask').onclick = function () {
if (/how do you feel today/i.test(document.getElementById('question').value)) {
document.getElementById('answer').replaceChild(document.createTextNode (mood.mood), document.getElementById('answer').firstChild);
document.bgColor = mood.color;
}
}
// -->
</script>
tomyknoker
06-28-2003, 09:17 AM
hi,
the script is great...it puts this symbol underneath the input box...Ê also how would I put the answer already in the box...ie "How do you feel today" and all they have to do is push submit?
Charles
06-28-2003, 09:38 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
<!--
Array.prototype.random = function () {return this[Math.round (Math.random() * Math.ceil (this.length / 10) * 10) % this.length]}
var mood = [{mood:"I'm tired", color:"blue"}, {mood:"I am happy", color:"yellow"}, {mood:"I am cold", color:"cyan"}, {mood:"I am hot", color:"maroon"}, {mood:"I am angry", color:"red"}, {mood:"I am sad", color:"orange"}].random();
document.write('<input type="text" id="question" value="How do you feel today?"><br><button id="ask">Submit</button>');
document.getElementById('ask').onclick = function () {
if (/how do you feel today/i.test(document.getElementById('question').value)) {
document.getElementById('question').value = mood.mood;
document.bgColor = mood.color;
}
}
// -->
</script>