SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    PHP Developer W1LL's Avatar
    Join Date
    Apr 2001
    Location
    Leicester, UK
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Text Box default text

    Probably a simple query here... how do you have a default value in a text box, then when the text box has focus it goes empty?

    Any help appreciated!

    - Will

  2. #2
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    onfocus=document.myForm.textareaName="" I think will work
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  3. #3
    PHP Developer W1LL's Avatar
    Join Date
    Apr 2001
    Location
    Leicester, UK
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That doesn't work... anyone else?

    - Will

  4. #4
    PHP Developer W1LL's Avatar
    Join Date
    Apr 2001
    Location
    Leicester, UK
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I figured it out!

    Code:
    onFocus="document.formName.textboxName.value=''"
    - Will

  5. #5
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's more verbose than necessary

    onfocus="this.value=''";

    However, what happens if someone enters a valid value, then re-focuses on the field? Whoops! You just deleted their entry - which will likely aggravate them into leaving. Here's a better solution -- first this, somwhere in a <script> block or an external JS
    Code:
    function focusHandler( elem )
    {
       if ( elem.value == elem.defaultValue ) elem.value = '';
    }
    function blurHandler( elem )
    {
       if ( elem.value == '' ) elem.value = elem.defaultValue;
    }
    And, then this
    Code:
    <input type="text" value="blah" onfocus="focusHandler( this )" onblur="blurHandler( this )"/>
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





Bookmarks

Posting Permissions

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