Javascript 'value' Property
Solution 1:
The value
attribute defines a default value which will be displayed in the element on page load. Form controls and some other html elements have this attribute (see list below).
The value
attribute in HTML and the value
property in JavaScript work differently for these controls. You can set the initial value with the value attribute, but the value property contains the actual value of the control. Source
The textContent
property sets or returns the textual content of the specified node, and all its descendants. If you set the textContent
property, any child nodes are removed and replaced by a single Text node containing the specified string.
To set or return the HTML content of an element, use the innerHTML
property instead.
In HTML DOM, the value
property sets or returns the value of the attribute:
- For
button
,input
andoption
elements, thevalue
attribute specifies the initial value of the element. - For
li
elements, thevalue
attribute sets the value of an ordered list item. The following list items will increment from that number. - For
progress
elements, thevalue
attribute specifies how much of the task has been completed. - For
param
elements, thevalue
attribute specifies the value of a element.
HTML elements with value
attribute: SourceSource
button
input
button
: type="button"checkbox
: type="checkbox"file
: type="file"hidden
: type="hidden"password
: type="password"radio
: type="radio"reset
: type="reset"submit
: type="submit"text
: type="text"
option
li
progress
param
meter
Solution 2:
Briefly:
All the elements which allow a user to type something in or select something, have a value
property with js.
If the elements have just plain text, you can usetextContent
and innerHTML
to get the plain text value
Solution 3:
value
is a property of form field elements. Paragraphs (and other elements) have textContent
and innerHTML
.
It is closely coupled with a corresponding value
attribute on the field - in other words, if one is updated, so is the other.
Post a Comment for "Javascript 'value' Property"