Funky Munky > Examples > Quick-Color > Script Explanation
.
The user types in a hexadecimal number, and then the background becomes that color. Here is the form:

Note: Lines are numbered for clarification.

1
2
3
<form name="a" onSubmit="change(document.a.b.value); return false;">
<input type="text" name="b" size=20>
</form>

So, there's the form. When you enter something into document.a.b on line #2 and hit enter, it does the onSubmit defined on line #1, which calls up the function change(), with the textbox's value as the parameters.

The script:

1
2
3
4
5
6
7
8
9
10
11
12
<script>
<!--
function change(x) {
  if (x.indexOf("#") == -1) {
    x = "#" + x;
                             }
  document.bgColor=x;
  document.a.b.value = '';
  document.a.b.select();
}
//-->
</script>

On line #4, it sees if there's a pound (#) sign. If there isn't, then it adds one on line #5. If there is, it leaves the variable 'x' as it is. Then it sets the document bgcolor to the variable 'x' on line #7. Next, it clears the textbox on line #8, and on line #9 it selects it.

[ Funky Munky ] [ Back to main page ] [ Examples and Experiments ]