Input Text value attribute
Definition and usage
value
Attribute setting or returning the value of the text field's value attribute.
The HTML value attribute contains a default value or the value entered by the user (or the value set by the script).
See also:
HTML Reference Manual:Propriedade value do <input> HTML
Instance
Example 1
Change the value of the text field:
document.getElementById("myText").value = "Bill Gates";
More examples are provided below the page.
Syntax
Return the value attribute:
textObject.value
Set the value attribute:
textObject.value = text
Attribute value
Value | Description |
---|---|
text | Specify the value of the input text field. |
Technical details
Return value: | A string value representing the value of the text field. |
---|
Mais exemplos
Exemplo 2
Obter o valor do campo de texto:
var x = document.getElementById("myText").value;
Exemplo 3
Validação de formulário:
var at = document.getElementById("email").value.indexOf("@"); var age = document.getElementById("age").value; var fname = document.getElementById("fname").value; submitOK = "true"; if (fname.length > 10) { alert("The name may have no more than 10 characters"); submitOK = "false"; if (isNaN(age) || age < 1 || age > 100) { alert("The age must be a number between 1 and 100"); submitOK = "false"; if (at == -1) { alert("Not a valid e-mail!"); submitOK = "false"; if (submitOK == "false") { return false;
Exemplo 4
Lista suspensa no formulário:
var mylist = document.getElementById("myList"); document.getElementById("favorite").value = mylist.options[mylist.selectedIndex].text;
Exemplo 5
Outra lista suspensa:
var no = document.getElementById("no"); var option = no.options[no.selectedIndex].text; var txt = document.getElementById("result").value; txt = txt + option; document.getElementById("result").value = txt;
Exemplo 6
Exemplo de diferença entre os atributos defaultValue e value:
var x = document.getElementById("myText"); var defaultVal = x.defaultValue; var currentVal = x.value;
Suporte ao navegador
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
Suporte | Suporte | Suporte | Suporte | Suporte |