Latest web development tutorials
 

Input Text defaultValue Property

< Input Text Object

Example

Change the default value of a text field:

document.getElementById("myText").defaultValue = "Goofy";
Try it Yourself »

Definition and Usage

The defaultValue property sets or returns the default value of a text field.

Note: The default value is the value specified in the HTML value attribute.

The difference between the defaultValue and value property, is that defaultValue contains the default value, while value contains the current value after some changes have been made. If there are no changes, defaultValue and value is the same (see "More Examples" below).

The defaultValue property is useful when you want to find out whether the contents of a text field have been changed.


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The defaultValue property is supported in all major browsers.


Syntax

Return the defaultValue property:

textObject.defaultValue

Set the defaultValue property:

textObject.defaultValue=value

Property Values

Value Description
value Specifies the default value of the text field

Technical Details

Return Value: A String, representing the default value of the text field

More Examples

Example

Get the default value of a text field:

var x = document.getElementById("myText").defaultValue;

The result of x will be:

Mickey
Try it Yourself »

Example

An example that shows the difference between the defaultValue and value property:

var x = document.getElementById("myText");
var defaultVal = x.defaultValue;
var currentVal = x.value;
Try it Yourself »

< Input Text Object