Results 1 to 4 of 4

Thread: Form Query

  1. #1
    Join Date
    Jul 2007
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Form Query

    hello. i have a query about the <form> tag.

    what i'm looking to do is, i have a search form on my site. for the purposes of the client this input box is filled with the words "keyword search" in order to guide them.

    what i want to know is how can i code it so that when the client clicks on the input box to enter their search the "keyword search" automatically disappears and they don't have to manually delete the phrase.

    can anyone help?

    very much appreciated

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    <input
      type="text"
      onfocus="this.value === this.defaultValue && (this.value = '');"
      onblur="this.value = this.value || this.defaultValue;"
      name="search"
      value="keyword search"
    >
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    Quote Originally Posted by Twey View Post
    Code:
    <input
      type="text"
      onfocus="this.value === this.defaultValue && (this.value = '');"
      onblur="this.value = this.value || this.defaultValue;"
      name="search"
      value="keyword search"
    >
    that would be a *beep* to type out every time.
    Code:
    <script type="text/javascript">
    function clearDefault(el) {
           if (el.defaultValue==el.value) el.value = ""
    }
    function restoreDefault(el) {
           if (el.value!=el.defaultValue || el.value="")el.value = el.defaultValue
    }
    </script>
    Code:
    <input type="text" name="something" onfocus="clearDefault(this)" onblur="restoreDefault(this)">

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    It's not that long (lengthened somewhat in your version, however). I also like to avoid making functions out of anything that'll fit in a single statement where possible, except for semantics and compatibility.

    I suspect that you've made an error here:
    Code:
    if (el.value!=el.defaultValue || el.value="")el.value = el.defaultValue
    This will erase the user's entry every time s/he removes focus from the input (e.g. in order to click the submit button). I doubt this is what was intended.

    Ideally, the whole code would be made unobtrusive and apply the handlers dynamically based on a CSS class. However, that's much too complex for a simple example such as this.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •