Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Javascript help please--visitor changes bg and font color  (Read 4167 times)

0 Members and 1 Guest are viewing this topic.

abyssofdawn

  • Guest
Javascript help please--visitor changes bg and font color
« on: October 30, 2009, 02:02:34 PM »
I'm trying to make it so that there is a form in which a visitor to the web page can input RGB values and click submit to change the background and text colors. This is what I have so far, but when I click submit it reloads to a page with the background color but nothing else. Does anybody know what's wrong with my script? Thank you.



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<script type="text/javascript">
 function rainbowfy()
  {
    var red1 = (document.changeColors.redBg.value);
    var green1 = (document.changeColors.greenBg.value);
    var blue1 = (document.changeColors.blueBg.value);

    var red2 = (document.changeColors.redTxt.value);
    var green2 = (document.changeColors.greenTxt.value);
    var blue2 = (document.changeColors.blueTxt.value);
     
       document.write("<body bgcolor='#" + red1 + green1 + blue1 + "'>");
       document.write("<font color='#" + red2 + green2 + blue2 + "'>");
  }
</script>

<script type="text/javascript">
 function get_random()
 {
  var ranNum=Math.floor(Math.random()*5);
  return ranNum;
 }
  function getaQuote()
  {
   var whichQuote=get_random();

   var quote=new Array(5)
   quote[0]="You may be deceived if you trust too much, but you willl live in torment if you do not trust enough. -Frank Crane";
   quote[1]="I am extraordinarily patient, provided I get my own way in the end. -Margaret Thatcher";
   quote[2]="The deepest definition of youth is life as yet untouched by tragedy. -Alfed North Whitehead";
   quote[3]="Always remember others may hate you but those who hate you don't win unless you hate them. And then you destroy yourself. -Richard M. Nixon";
   quote[4]="Always forgive your enemies. Nothing annoys them so much. -Oscar Wilde";

     document.write(quote[whichQuote]);
   }
</script>

</head>

<body>

<h2>Select Colors</h2>

 <form name="changeColors">

  Background (enter values from '00' to FF')

   RED:<input name="redBg" type="text" style="width: 30px;">
   GREEN:<input name="greenBg" type="text" style="width: 30px;">
   BLUE:<input name="blueBg" type="text" style="width: 30px;">


  Text (enter values from '00' to 'FF')

   RED:<input name="redTxt" type="text" style="width: 30px;">
   GREEN:<input name="greenTxt" type="text" style="width: 30px;">
   BLUE:<input name="blueTxt" type="text" style="width: 30px;">


  <input name="apply" type="button" value="Submit" onclick="rainbowfy();" />

 </form>
 <script type="text/javascript">
 getaQuote();
 </script>
</body>

</html>

trevorpe



    Beginner

    Thanked: 3
    • Yes
  • Computer: Specs
  • Experience: Experienced
  • OS: Linux variant
Re: Javascript help please--visitor changes bg and font color
« Reply #1 on: November 13, 2009, 01:18:10 PM »
When changing the background color, use the Javascript syntax and not the "document.write()" function.

Use this
Code: [Select]
document.style.body.backgroundColor="#000000";
//Just make it so that the value above is what the user wants
//You could try a variable set to the right color
Trevor P.