Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onclick to be invoked only once

Status
Not open for further replies.

waydown

Programmer
Apr 27, 2009
49
GB
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.
 
I see 2 options:

1. You can check if the text inside is the initial text, and only then clear it.

Code:
onclick="if(this.value=='Initial Text'){this.value=';';}"


2.You could disable the onclick once its been used once.

Code:
onclick="this.value=';'; this.onclick='';"


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Hi

In case you want a placeholder text, your approach is wrong. The [tt]input[/tt] can receive the focus without clicking it, in which case your event handler will not be triggered. For a placeholder text better use [tt]onfocus[/tt]. And personally I would also use [tt]onblur[/tt] to restore the placeholder if nothing was entered :
Code:
[b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"text"[/i][/green] [maroon]name[/maroon][teal]=[/teal][green][i]"myText"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]"Initial text"[/i][/green] [maroon]onfocus[/maroon][teal]=[/teal][green][i]"if(this.value=='Initial text')this.value=''"[/i][/green] [maroon]onblur[/maroon][teal]=[/teal][green][i]"if(this.value=='')this.value='Initial text'"[/i][/green][b]>[/b]


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top