Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    onclick to be invoked only once

    Hi,
    I have created the following text input field:
    Code:
    <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.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,495 Times in 4,459 Posts
    Code:
    <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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by Old Pedant View Post
    Code:
    <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 = '';"


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •