Полезная информация

JavaScript: The Definitive Guide

Previous Chapter 21
JavaScript Reference

Element.value Property


Element.value Property---value displayed or submitted by a form element


Navigator 2.0, buggy in Internet Explorer 3.0




value is a read/write string property of all form elements. It specifies the value that is displayed by the form element and/or submitted for the element when the form is submitted. The value property of the Text element, for example, is the user's input, which is also the value submitted with the form. For the Checkbox object, on the other hand, the value property specifies a string that is not displayed, but which will be submitted with the form if the Checkbox element is checked when the form is submitted.

The initial value of the value property is specified by the VALUE attribute of the HTML tag that defines the form element.

For Button, Submit, and Reset objects, the value property specifies the text to appear within the button. On some platforms, changing the value property of these elements will actually change the text displayed by the buttons on-screen. This does not work on all platforms, however, and is not an advisable technique because changing the label of a button may change the size of the button, and cause it to overlap and obscure other portions of the document.

The Select element has a value property, like all form elements, but does not use it. Instead, the value submitted by this element is specified by the value property of the Option objects it contains.

In Navigator 2.0 and Internet Explorer 3.0, the value property of the Password object is nonfunctional for security purposes. It can be read and written, but this has no bearing on the value entered by the user, or submitted with the form. In Navigator 3.0, when the data-tainting security model is enabled, the value property does actually contain the user's input to this element.

A limitation of Internet Explorer 3.0 only allows strings to be assigned to the value property of HTML form elements. If you want to assign objects to this property, you must explicitly convert them to strings by, for example, adding the empty string to them. Thus, to display the current date and time in a text field, Internet Explorer would require code like this:

var today = new Date();
document.forms[0].elements[1].value = "" + today;


Internet Explorer 3.0 does not automatically convert objects to strings when they are assigned to this property; you must explicitly perform that conversion yourself.

See Also

"Element", "Element.name"

Previous Home Next
Element.type Book Index Embed

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell