Saturday, 17 August 2013

style.color if-statement not working

style.color if-statement not working

I'm trying to make some javascript for entering/exiting (onfocus/onblur)
textfields. I want to have faded "pretyped" text which disappears when you
enter the field. The problem is testing to see if the value of the textbox
should be removed or not. One way to is to check the value of the
textfield upon entering, and if it's equal to the pretyped text, then the
value is set to nothing, like so:
if(field.value=='username'){
field.value=''; //removes pretyped text
field.style.color='rgb(0, 0, 0,)'; //turns text black
}
However, the user can replicate the pretyped text, and upon entering that
text, it will also be removed. I guess this is a minor problem, but I want
to be as professional as possible. To fix this problem, I tried testing
for the color of the text in the textfield, because the user is not able
to replicate the color of the grayed pretyped text. I used this code:
if(field.style.color=='rgb(150, 150, 150)'){ //default color in CSS
field.value=''; //removes pretyped text
field.style.color='rgb(0, 0, 0,)'; //turns text black
}
This doesn't work for some reason. The pretyped text remains, and the
color doesn't change. I'm pretty sure that the syntax for accessing the
CSS color is correct (field.style.color), as it works to set the color to
black (but only when testing for the value of the field).
Any help is appreciated :)

No comments:

Post a Comment