Forum Moderators: open

Message Too Old, No Replies

Forms: textarea clear on focus

         

DougWD

8:07 am on Nov 13, 2007 (gmt 0)

10+ Year Member



What the best way to clear a message in a text area on focus, but allow it to come back on browser refresh? Or, simply the most cross browser compliant method of clearing the text on focus--or any other way of which I'm not aware.

Thanks.

phranque

10:11 am on Nov 13, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



try this:

<textarea rows="16" cols="20" name="clarence" onFocus="this.value=''">clearance</textarea>

penders

4:13 pm on Nov 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



However, you probably want to prevent the control being cleared when the user goes back to the <textarea> the 2nd time, as it will clear what they have already typed.

Modifying phranque's code:

onfocus="this.value=''; this.onfocus=null"

This just clears the event, so it won't fire more than once.

phranque

11:25 pm on Nov 13, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



this will do the same thing:
onFocus="if (this.value == this.defaultValue) { this.value = ''; }"

or you could put that in a function that does fancier things with onfocus after the initial value has been modified.

DougWD

7:11 am on Nov 14, 2007 (gmt 0)

10+ Year Member



I used this method before I asked the question because I didn't want to taint the "best" way to do it. lol

onFocus="this.value=''; this.onfocus=null;"

So I guess I found it after all. Thanks for all who responded.

One thing about this method is that it alerts IE that something may be awry with the page. The user has to "click" to allow content. It's just simple javascript, so what's the big deal with IE?

penders

8:31 am on Nov 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



One thing about this method is that it alerts IE that something may be awry with the page. The user has to "click" to allow content. It's just simple javascript, so what's the big deal with IE?

...as with any JavaScript you are trying to run locally, offline - it's a 'security' warning. If you upload your page to a webserver you will not get this message.

Alternatively, to run your content locally without the warning, you can add a "Mark of the Web" (MOTW) to your page, to make it run in a different security zone. This takes the form of an HTML comment you place below the DOCTYPE. The general form is:

<!-- saved from url=(0014)about:internet -->

Google for "MOTW".

Or you can alter the security settings in your browser (Options > Advanced > Allow active content to run in files on My Computer) - unwise.

DougWD

9:09 am on Nov 14, 2007 (gmt 0)

10+ Year Member



OK thanks much for that.