...

View Full Version : onclick to be invoked only once



raydona
11-10-2010, 01:26 AM
Hi,
I have created the following text input field:

<form name="xyz">
<input type="text" name="myText" size="30"
value="Initial text"
onclick="document.xyz.myText.value='';"/>
</form>
When the field is clicked the initial text is removed, clearing the way for new text to be entered, which is what I want. The trouble is if the field is again clicked the new text entered is also cleared which is not what I want. I only the field cleared for the first click and not for subsequent clicks. How can this be achieved? I will be grateful for all help. :D

Old Pedant
11-10-2010, 01:34 AM
<input type="text" name="myText" size="30"
value="Initial text"
onclick="if ( this.value == 'Initial text' ) this.value = '';" />


You might want to consider using onfocus in place of onclick.

Logic Ali
11-10-2010, 04:01 AM
<input type="text" name="myText" size="30"
value="Initial text"
onclick="if ( this.value == 'Initial text' ) this.value = '';" />


You can make that more maintainable:onclick="if ( this.value == this.defaultValue ) this.value = '';"



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum