Forum Moderators: open

Message Too Old, No Replies

Form Field Initial Content

         

almo136

11:29 pm on Jul 17, 2009 (gmt 0)

10+ Year Member



Hi,

I'd like a field in a form which has some initial text but when you click in the field the content erases itself. I've seen this done a lot but don't know how to do it.

Any pointers?

astupidname

9:53 am on Jul 18, 2009 (gmt 0)

10+ Year Member



Something like the following is what I believe you may want:

<div style="width:500px;">
<form action="" method="get">
If the field below receives focus, the default text will clear out.
When the field loses focus, if user has not entered any text in to the box,
it will revert back to it's original value,
else it will keep whatever value user has entered.
Remove the onblur attribute if you don't want it to revert back to it's original value in the event of non-entry by user<br>
<input type="text" value="some default value" onfocus="this.value='';" onblur="if (this.value == '') {this.value = this.defaultValue;}">
</form>
</div>

astupidname

10:01 am on Jul 18, 2009 (gmt 0)

10+ Year Member



Actually, you might want to make it like this instead (else if a user enters text then focuses away from the input and then refocuses on the input the previously user-entered text would disappear -which we probably don't want to happen as the user may just desire to 'tweak' their entry) :

<input type="text" value="some default value" onfocus="if (this.value == this.defaultValue) {this.value='';}" onblur="if (this.value == '') {this.value = this.defaultValue;}">