Click to See Complete Forum and Search --> : Clicking makes text disappear


MacMyDay
08-05-2003, 05:21 AM
I've seen this before, yet can't remember where to get the source from. But, it had a value in the text box yet when clicking on it the text disappeared and you could carry on typing. Can anyone offer the code? Thanks

requestcode
08-05-2003, 06:41 AM
Something like this?
<input type="text" name="txta" value="some text" onClick="this.value=''">

MacMyDay
08-05-2003, 06:43 AM
Perfect. Thanks.

Charles
08-05-2003, 06:50 AM
That example has one little problem. Every time the user clicks on the field it will be cleared. You might want to use instead:

<input type="text" name="txta" value="some text" onclick="this.value=''; this.onclick = function () {}">

MacMyDay
08-05-2003, 09:09 AM
This is probably gonna sound really stupid, but how do I modify that for <textarea></textarea>

AdamBrill
08-05-2003, 09:22 AM
Originally posted by Charles
That example has one little problem. Every time the user clicks on the field it will be cleared. You might want to use instead:

<input type="text" name="txta" value="some text" onclick="this.value=''; this.onclick = function () {}"> The problem with this is that if they had it do something else onclick, then it would clear that, too. Correct me if I'm wrong, but I think this is better:<input type="text" name="txta" value="some text" onclick="(this.clicked)?'':this.value=''; this.clicked=true;">MacMyDay, you would put it in a textarea like this:<textarea name="txta" onclick="(this.clicked)?'':this.value=''; this.clicked=true;">Some text</textarea>

Charles
08-05-2003, 09:29 AM
That works as well, though it's only going to be better if you're doing other things with the onclick. And you could modify my version so that the other thing was assigned to the handler. Or you could use the "onfocus" handler.

Also, because of JavaScript's automatic garbage collection I tend to avoid creating properties and variables.

requestcode
08-05-2003, 10:02 AM
Another option would be to select the text when focus placed on the text box then when they start typing it would erase the text in their. If they don't want the text erased then they could alwas just move the cursor with the cursor key.
<input type="text" name="txta" value="some text" onFocus="this.select()">

You could also do the same for a textarea.