Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Highlight search box text

  1. #1
    Join Date
    Jun 2007
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Highlight search box text

    hi

    what do i have to do to a search box form, so that whenever you click on the box, whatever text is there gets selected and the user can just hit backspace or delete to clear the whole box?

    alternatively, is there any way to mimic the OS X style search bar, where you have a 'clear' button on the right of the bar? like this:


    thanks for any help!

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    HTML Code:
    <input type="text" onclick="this.select();" value="search">
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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

    Default

    The user just has to double-click it on all systems I've used...
    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!

  4. #4
    Join Date
    May 2007
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I agree with jscheur1, but here's a little more to that code that when they click in the box the text disappears. If they don't type anything the text reappears when they click off the box.

    <input type="text" onfocus="if(this.value=='Text')this.value=''" onblur="if(this.value=='')this.value='Text'" value="Text" />

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

    Default

    Hmm. A bit of redundancy there. You can eliminate it by using the defaultValue property:
    Code:
    <input
      type="text"
      onblur="this.value = this.value || this.defaultValue;"
      onfocus="this.value = '';"
      value="Text"
    >
    Stay away from XHTML at least until IE gets support.
    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!

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey View Post
    Hmm. A bit of redundancy there. You can eliminate it by using the defaultValue property:
    Code:
    <input
      type="text"
      onblur="this.value = this.value || this.defaultValue;"
      onfocus="this.value = '';"
      value="Text"
    >
    Stay away from XHTML at least until IE gets support.
    I almost said this about eleven82's idea until I realized that it would all work out more or less, as any value I entered would be preserved unless I changed it. However, with this new twist, if I enter a value, blur the element, and then focus on it again, my value will be lost.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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

    Default

    True... perhaps:
    Code:
    <input
      type="text"
      onblur="this.value = this.value || this.defaultValue;"
      onfocus="this.value == this.defaultValue && this.value = '';"
      value="Text"
    >
    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!

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey View Post
    True... perhaps:
    Code:
    <input
      type="text"
      onblur="this.value = this.value || this.defaultValue;"
      onfocus="this.value == this.defaultValue && this.value = '';"
      value="Text"
    >
    That doesn't do anything, as far as I can see, that is different than an ordinary text input. Try:

    Code:
    <input
      type="text"
      onblur="this.value = this.value || this.defaultValue;"
      onfocus="this.value = this.value == this.defaultValue? '' : this.value;"
      value="Text"
    >
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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

    Default

    That code performs exactly the same function as mine, as far as I can see. Have I missed something?
    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!

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey View Post
    That code performs exactly the same function as mine, as far as I can see. Have I missed something?
    Testing in Opera? That is all I did and the difference there is like I said. Probably would be in others.

    eleven82's not too pretty code worked fine too. None of this was what the OP asked for though.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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
  •