Input Text defaultValue attribute

Definition and usage

defaultValue Sets or returns the default value of a text field.

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

The difference between defaultValue and value properties is that defaultValue contains the default value, while value contains the current value after some changes. If there are no changes, defaultValue and value are the same (see the example below).

The defaultValue property is very useful if you want to check whether the content of the text field has changed.

Example

Example 1

Change the default value of a text field:

document.getElementById("myText").defaultValue = "Steve";

Try it yourself

Example 2

Get the default value of a text field:

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

Try it yourself

Example 3

Example to show the difference between defaultValue and value properties:

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

Try it yourself

Syntax

Return defaultValue property:

textObject.defaultValue

Indstil defaultValue egenskab:

textObject.defaultValue = value

Egenskabsværdi

Værdi Beskrivelse
value Definerer standardværdien for tekstdatafeltet.

Tekniske detaljer

Returværdi: Strengværdi, der angiver standardværdien for tekstdatafeltet.

Browserstøtte

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Support Support Support Support Support