Javascript Variable question

  • Blackdeath
  • Novice
  • Novice
  • User avatar
  • Posts: 18
  • Loc: Wisconsin

Post 3+ Months Ago

My ultimate goal is to have a text box where you can enter in a hexidecimal value for a color, then have a button which you can hit and bam the background changes to that color. I though I had it right, I thought wrong eheh. This is the Javascript I am using looks like this.
Code: [ Select ]
<script languange="Javascript">
var color=document.cgform..textz.value

function changecolor()


<form name="cgform">
<input type="text" name="textz" value="purple" size="16">

<input type="button" name="bgcolorchange" value="click and find out!" onClick="changecolor();return true">

  1. <head>
  2. <script languange="Javascript">
  3. <!----
  4. var color=document.cgform..textz.value
  5. function changecolor()
  6. {
  7. document.bgColor=color
  8. }
  9. ----->
  10. </script>
  11. </head>
  12. <body>
  13. <form name="cgform">
  14. <input type="text" name="textz" value="purple" size="16">
  15. </form>
  16. <input type="button" name="bgcolorchange" value="click and find out!" onClick="changecolor();return true">
  17. </body>

I was wondering if someone could shed some light on the situation and tell me what I am doing wrong here. The only thing I could think of is that you can't use the document.cgform.textz.value as a variable value, other than that I am lost.
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

You can assign it to a variable, but when you do it like that it won't work because any code outside the function will be processed before the page is fully loaded. In other words, you've tried to assign the value before the form and its fields have been created. Just move it into the function call becuase that won't run until the button on the form is clicked and everything will exist by then.

Code: [ Select ]
<script languange="Javascript">
function changecolor()
  1. <script languange="Javascript">
  2. <!--
  3. function changecolor()
  4. {
  5. color=document.cgform.textz.value;
  6. document.bgColor=color;
  7. }
  8. -->
  9. </script>

or just assign it directly to the bgcolor.

Code: [ Select ]
  • joebert
  • Genius
  • Genius
  • User avatar
  • Posts: 13511
  • Loc: Florida

Post 3+ Months Ago

Try examining the script i posted at the bottem of this post It might give you some ideas.

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 14 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum

© 1998-2017. Ozzu® is a registered trademark of Unmelted, LLC.