Text Box with Disappearing Text on Click

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JazzMaTick
    New Member
    • Jul 2007
    • 1

    Text Box with Disappearing Text on Click

    I want to make a website with an online contact form that when someone clicks on the text box the text "Name" disappears, but after the person goes ahead writes their real name clicks outside the text box and clicks back in the text box the name doesn't disappear.

    At the moment my text field deletes whatever is inside the textbox no matter if its user input or my predetermined value.

    Here is my current code:

    <INPUT NAME="name" TYPE="TEXT" CLASS="text_1" onFocus="this.v alue='';return false;" value="WRITE YOUR NAME HERE" SIZE="30">

    Any help would be great. If I should go a totally different approach please let me know :-) I am very new and inexperienced but I'm very eager to learn.

    **** - email removed (against site rules)
    Last edited by acoder; Jul 18 '07, 10:59 AM. Reason: removed email
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, JazzMaTick. Welcome to TSDN!

    Add onblur and onfocus handlers to the input:
    [code=javascript]
    var $defaultText = 'WRITE YOUR NAME HERE';

    function setVal($element , $if, $then)
    {
    if($element.val ue == $if)
    {
    $element.value = $then;
    }
    }
    [/code]

    [code=html]
    <input name="name" type="text" class="text_1" value="WRITE YOUR NAME HERE" size="30" onfocus="setVal (this, $defaultText, '');" onblur="setVal( this, '', $defaultText);" />
    [/code]

    Comment

    • anat
      New Member
      • Aug 2007
      • 1

      #3
      the easiest way would be:

      Code:
      <input onfocus="this.value='';" type="text" size="61" name="EMail" value="Type Your EMail Address Here">

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, anat. Welcome to TSDN!

        Originally posted by anat
        the easiest way would be:

        Code:
        <input onfocus="this.value='';" type="text" size="61" name="EMail" value="Type Your EMail Address Here">
        The only problem with that is that this would delete the contents of the input even if the User had already typed something in it.

        The idea behind my post was to check to see if the value of the input matched a given string before changing the contents.

        Comment

        • Logician
          New Member
          • Feb 2007
          • 210

          #5
          Originally posted by JazzMaTick
          I want to make a website with an online contact form that when someone clicks on the text box the text "Name" disappears, but after the person goes ahead writes their real name clicks outside the text box and clicks back in the text box the name doesn't disappear.
          Code:
          <input name="userName" type="text" class="text_1" onfocus="if(this.value==this.defaultValue)this.value=''" value="Write your name here" size="30">
          You could add an onblur handler to restore the default if nothing is entered:
          Code:
          onblur='if(!/\S/.test(this.value))this.value=this.defaultValue'
          Advice.

          Don't use the value "name".
          Get used to writing tag names in lowercase.

          Comment

          Working...