Default Value

Defualt Value is a fallback for the HTML5 placeholder attribute. It sets a default value on your input elements if they are empty. The default value disappears if the input gain focus or the user types something in it, even in browsers without support for the placeholder attribute.

The placeholder attribute is perfect for label values like “Search here…”, “Username” or example values like “john@doe.com”. The placeholder attribute and Default Value works with text-, password-inputs and textareas.

How to use Defaultvalue

Use the HTML5 placeholder attribute is you normally would.

  1. <input placeholder="Enter a username…" type="text">
  2. <input placeholder="…and a password" type="password">

Then initiate Default Value on those elements.

  1. $(' [placeholder] ').defaultValue();

Empty input fields has the class name ‘empty’

When the default value is shown the class name ‘empty’ is added to the field. Take a look at the demo and see how the default values differs from the user created values.

Default Value manipulates the DOM

Since you can’t change the type of an input field in Internet Explorer, Defaultvalue creates a clone of your password field but with the type text instead.

Change log
VersionChanges
1.4.2Fixed detection for native placeholder support and added two more attributes – row and cols – to the clone. (Thanks to marcus)
1.4.1Fixed an issue regarding detection for native placeholder support.
1.4Now uses the HTML5 attribute placeholder instead of rel
1.3.1Fixed Lazy compatibility (Thanks Anders Adlén)
1.3Refactored (Thanks Kristofer Karlsson)
1.2Fixed an ‘addClass’ is not a function error (Thanks pentarim)
1.1Refactored. Default values are now removed when closest form is submitted. Fixed tabindex issue. (Thanks a lot to Paul Elliott)

3 days ago

Twitter Bootstrap 2.0 is here. By nerds, for nerds. Cross everything. http://t.co/J4cFxGlb A neat looking front-end toolkit.

9 days ago

Looking for a solid way to do application versioning? Look no further. http://t.co/OMYJ2xzF Semantic Versioning guidelines are nifty!

15 days ago

On Amazon: Bad reviews are better for product sales than no reviews. (via @ronnestam )

15 days ago

There are only 180k Twitter users in Sweden, but ~75% of Sweden’s journalists use Twitter. (via @ronnestam )