SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist runeveryday's Avatar
    Join Date
    Jul 2009
    Posts
    437
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    javascript input box

    Code:
    <form id="commentform" method="post" action="post.php">
        <input type="text" aria-required="true" tabindex="1" size="22" value="vistor" 
           id="author" name="author">
    </form>
    I set default value "visitor" to the input box. When focus is in text box or mouse enters it, I want to hide "visitor" string and want to show it when it loses focus or mose moves out. how to write the javascript code. thank you.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    The web browser stores the starting value as a defaultValue property on the element, so we can make good use of that.

    Attach an onfocus and onblur event on to the author field within the form.

    Code javascript:
    var form = document.getElementById('commentform');
    form.elements.author.onfocus = hideDefaultValue;
    form.elements.author.onblur = showDefaultValue;

    On focus, the hideDefaultValue function removes the default value. If you've changed the field to something else, then the value that's there remains there.

    Code javascript:
    function hideDefaultValue {
        if (this.value === this.defaultValue) {
            this.value = '';
        }
    }

    On blur, the showDefaultValue functions checks to see if the form field is empty. If it is, it puts the default value there instead.

    Code javascript:
    function showDefaultValue {
        if (this.value === '') {
            this.value = this.defaultValue;
        }
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    You can also set and clear a className value on the field. That can be useful if you want the displayed default value to be shown in a different color such as light grey.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Evangelist runeveryday's Avatar
    Join Date
    Jul 2009
    Posts
    437
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    You can also set and clear a className value on the field. That can be useful if you want the displayed default value to be shown in a different color such as light grey.
    how to set the default value to be shown in a different color? thank you.
    2,what't this line meaning? form.elements.author.onfocus

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by runeveryday View Post
    how to set the default value to be shown in a different color? thank you.
    You would have a CSS declaration to specify the color:

    Code css:
    .default {
        color: gray;
    }

    Then as a part of the hideDefaultValue and showDefaultValue, you would set or remove that "default" class name on the form field.

    When you click on a field and the default value is hidden, the color should go from grey color back to its normal black color for normal text, so we remove the CSS class name on the field.

    Code:
    function hideDefaultValue {
        if (this.value === this.defaultValue) {
            this.value = '';
            this.className = '';
        }
    }
    When we show the default value, we set the CSS class name so that the appropriate CSS declaration can affect that field.

    Code:
    function showDefaultValue {
        if (this.value === '') {
            this.value = this.defaultValue;
            this.className = 'default';
        }
    }
    Quote Originally Posted by runeveryday View Post
    2,what't this line meaning? form.elements.author.onfocus
    "form" is the that variable contains a reference to the form.
    "elements" allows is to access the input fields of the form
    "author" is the name of the form field we wish to access
    "onfocus" is the event that triggers when click on field and can type in it
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •