Set/Clear Default Input Value

$('.default-value').each(function() {

       var default_value = this.value;

       $(this).focus(function(){
               if(this.value == default_value) {
                       this.value = '';
               }
       });

       $(this).blur(function(){
               if(this.value == '') {
                       this.value = default_value;
               }
       });

});

Comments

  1. User Avatar
    kartofelek

    This is very old snippet. Now you better use placeholder or equiwalent. And one important thing – whats about send value? You set the default, but sometimes this is wrong.

    • User Avatar
      Andre
      Permalink to comment#

      Considering ‘placeholder’ is part of HTML5, this JS method would be a safe fall back for older browsers…

      Not sure what the problem with the ‘send value’ is though; if the field is empty this will apply the input tag’s default ‘value’, if the field is not empty then it holds whatever was entered.

  2. User Avatar
    Jason Day
    Permalink to comment#

    With my form, I have fields that are not required but have pre-filled value text as a usability hint. Several of these fields go through various types of validation (right number of characters, etc).

    How would i clear only the default value of those specific fields?

    example:

    <input value="Enter promotion code" maxlength="40" title="Promotion Code 1">

    I need to clear the “Enter promotion code” on submit so that the value doesn’t go through validation and trigger an alert.

    • User Avatar
      Andre
      Permalink to comment#

      If you are aiming at newer browsers (or if accommodating older browsers is not a problem) it would be better to set it up like this:
      [code][/code]

      Although, to use this JS method, include the provided script in your page and use the following:
      [code][/code]

    • User Avatar
      Andre
      Permalink to comment#

      My bad… Too used to forum code tags.

      For HTML5 ‘placeholder’ method:

      For JS ‘value’ method:

    • User Avatar
      Andre
      Permalink to comment#

      What the heck? Ok… Let’s just do it this way: For this JS method just add class=”default-value” to the input tag and leave everything else the way you have it.

    • User Avatar
      Jason Day
      Permalink to comment#

      Unfortunately, this has to work in ie6 as well. Currently I have a span underneath the input field to simulate the functionality. But I don’t want to do it that way.

      The two forms where I’m using this are one input forms. For the submit, would it be possible to throw out the default value using onclick or onsubmit?

  3. User Avatar
    Jeff Shamley
    Permalink to comment#

    Perfect! Exactly what I was looking for. Thanks for the snippets.

Submit a Comment

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

icon-anchoricon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag