Click to See Complete Forum and Search --> : Change backgroundcolor of a text field... with colors from an Array.


Nayias
12-25-2003, 05:00 PM
Hey all, im trying to make a script that change the background color of a tekstfield when you type a number in that field.
this is the code i have till now:

function changetocolor(field)
{
var hello = new Array();
hello[0] = "blue";
hello[1] = "red";
hello[2] = "green";
hello[3] = "yellow";
hello[4] = "orange";
hello[5] = "black";
hello[6] = "purple";
hello[7] = "pink";
var color_number = parseInt(field.value);
field.style.background-color=hello[color_number];
}


then the user should type a color between 0 and 7,
so if the user types 4 the background color of the tekstfield should change to "orange".
I use the onChange event handler to call the function:

<input type="tekst" onChange="changetocolor(this)" >


im not sure if you can change the background color with: background-color
so i tried this:
backgroundColor
but that didnt work too.
can someone tell me why it doesnt work?

thx

fredmv
12-25-2003, 05:32 PM
<script type="text/javascript">
//<![CDATA[
function change(f)
{
var a = ['blue', 'red', 'green', 'yellow', 'orange', 'black', 'purple', 'pink'], c = a[parseInt(f.value, 10)];
f.style.backgroundColor = (typeof c != 'undefined') ? c : '#fff';
}
//]]>
</script><input type="text" onkeyup="change(this);" />

Nayias
12-25-2003, 06:31 PM
Thx it works fine :).

but can someone tell me what was wrong with my script?
why is it impossible with onChange event handler.

thx

fredmv
12-25-2003, 06:35 PM
It is possible with the onchange event handler but you just weren't seeing it be fired. The onchange event will fire after you've changed the value of the field and then click outside of the field.

Nayias
12-26-2003, 05:24 AM
oh yea, i c.
thx very much :)