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 valueattribute in HTML and the valueproperty 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,inputandoptionelements, thevalueattribute specifies the initial value of the element. - For
lielements, thevalueattribute sets the value of an ordered list item. The following list items will increment from that number. - For
progresselements, thevalueattribute specifies how much of the task has been completed. - For
paramelements, thevalueattribute specifies the value of a element.
HTML elements with value attribute: SourceSource
buttoninputbutton: 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"
optionliprogressparammeter
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 valueattribute on the field - in other words, if one is updated, so is the other.
Post a Comment for "Javascript 'value' Property"