Results 1 to 10 of 10

Thread: Text field

  1. #1
    Join Date
    Oct 2007
    Posts
    47
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Text field

    Hello people

    How do I manipulate a text field so that it the text I want in it is automatically deleted when typing in the field commences?

    Cheers
    hf

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    <input type="text" value="default" onFocus="if(this.value=='default'){this.value='';}" onBlur="if(this.value=''){this.value=='default';}">
    There might be a better way by making the string 'default' a variable you can change at one place in the code, but that's the simplest way to code this.

    At least I think that was your question.
    Last edited by djr33; 11-26-2007 at 01:13 PM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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

    Default

    Code:
    <input
        type="text"
        value="blaaaaaaaaaah!"
        onfocus="this.value === this.defaultValue && (this.value = '');"
        onblur="this.value = this.value || this.defaultValue;">
    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
    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
    onfocus="this.value === this.defaultValue && (this.value = '');"
    Twey, how does that equate to deleting the default value once focused on? or maybe I misunderstood the question? but by Daniel's response I do not believe I did.

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    The second line works because it will be the second term (ie the default value) if the first term evaluates to false.
    But the first line, which you quoted, does confuse me as well. I assume it works, but I haven't seen that syntax before. I think he's just having fun with our minds
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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

    Default

    I think he's just having fun with our minds
    *super-villain-Steve laugh*
    But the first line, which you quoted, does confuse me as well.
    Same principle -- if this.value === this.defaultValue is true, (this.value = '') is evaluated. It works because JS is lazy: if this.value !== this.defaultValue, then the expression as a whole can never be true, so it doesn't even bother evaluating the rest.
    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!

  7. #7
    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
    if this.value === this.defaultValue is true, (this.value = '') is evaluated.
    Yes I got the evaluation. what I was confuseda bout was the && which is from an operand. So in verbose terms I read that line as

    if (this value) is equal to the default value AND (this value) equals nothing
    Which to me would seem like it would toss an error because there is an equation being performed but no aoperation based upon the equation.. Again this may just be my lack of knowledge of Javascript, but was just looking for some clarification


    It works because JS is lazy: if this.value !== this.defaultValue, then the expression as a whole can never be true, so it doesn't even bother evaluating the rest.
    I did pick up on that, because both statements would need to be evaluate as true for the operation to be performed.... (operation , ya that was my first question)


    Quote Originally Posted by twey
    onblur="this.value = this.value || this.defaultValue;"
    Quote Originally Posted by djr
    The second line works because it will be the second term (ie the default value) if the first term evaluates to false.
    I understood that portion, which is why I only quoted the first statement and not both, but I am sure that someone is reading this now or will in the future and have that question so good pointing out

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

    Default

    if (this value) is equal to the default value AND (this value) equals nothing
    Uh-uh. That's assignment, not comparison. It doesn't translate well into English, but something like: if the value equals the default value AND the result of assigning an empty string to the value is truthy, and then yes indeed, we do nothing with the result of the expression (the side-effect of assignment is the important part of it).
    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!

  9. #9
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Quote Originally Posted by Twey View Post
    *super-villain-Steve laugh*
    lol. Having too much fun there with your new name?
    Trinithis

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

    Default

    well that's one I will have to wrap my head around a few times, but thanks for the translation sir.

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
  •