Input Text value attribute
Definition and usage
value
Set or return the value of the text field's value attribute.
The HTML value attribute contains a default value or a user input value (or a value set by the script).
See also:
HTML Reference Manual:Guna properti value <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 value attribute:
textObject.value
Set value attribute:
textObject.value = text
Attribute value
Value | Description |
---|---|
text | Specify the value of the input text field. |
Technical details
Return value: | String value, represents the value of the text field. |
---|
Lebih banyak contoh
Contoh 2
Mendapatkan nilai medan teks:
var x = document.getElementById("myText").value;
Contoh 3
Pengesahan borang:
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("Nama mungkin tidak boleh melebihi 10 aksara"); submitOK = "false"; } if (isNaN(age) || age < 1 || age > 100) { alert("Umur mesti nombor antara 1 dan 100"); submitOK = "false"; } if (at == -1) { alert("Bukan e-mel yang sah!"); submitOK = "false"; } if (submitOK == "false") { return false; }
Contoh 4
Senarai pilihan dalam borang:
var mylist = document.getElementById("myList"); document.getElementById("favorite").value = mylist.options[mylist.selectedIndex].text;
Contoh 5
Lagi senarai pilihan turun:
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;
Contoh 6
Paparan contoh perbezaan antara atribut defaultValue dan value:
var x = document.getElementById("myText"); var defaultVal = x.defaultValue; var currentVal = x.value;
Pendukung pelayar
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
Sokongan | Sokongan | Sokongan | Sokongan | Sokongan |