Javascript 'value' property

I need to know which Javascript Elements have a "Value" property associated with them? According to MDN, paragraphs don't have "Value" property but have "textContent" property. Is "Value" only used for manipulating text content of input type controls?

Answers:

Answer

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 and option elements, the value attribute specifies the initial value of the element.
  • For li elements, the value attribute sets the value of an ordered list item. The following list items will increment from that number.
  • For progress elements, the value attribute specifies how much of the task has been completed.
  • For param elements, the value attribute specifies the value of a element.

HTML elements with value attribute: Source Source

  • 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
Answer

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.

Answer

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

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.