Extracting information about objects

Various propertiesA slot for one of the values bundled in an object. and methodsA function associated with a class. can be used to obtain information about nodesA part of a tree.. Some of these properties and methods are applicable only to certain types of nodes.

Property innerHTML

The value of this property is a string containing the entire HTML content of an element. In this example:

<body>
  <p>One</p>
  <p>Two</p>
</body>

the value of document.body.innerHTML is a string with all the text between the start tagA marker showing the beginning of an element. and end tagA marker showing the end of an element. of the body elementAn HTML element that contains all the content of the page, tag name <code>body</code>., including the tags for the paragraph elements and the line breaks.

Property textContent

The value of this property is a string formed by the concatenationTo make a new one by gluing together two. of just the text (no HTML element names included) of all the descendantsA child or child of a child or so on. of the node. In the example above, for either of the paragraph nodes, the values of textContent and innerHTML will be identical. In contrast, unlike the value of document.body.innerHTML, the value of document.body.textContent will not contain the tags for the paragraph elements.

Property style

The propertyA slot for one of the values bundled in an object. style gives the style of a node, and a particular attributeExtra information added to an element. can be specified by using dot notationSyntax for a function call in which the first input is followed by a period and then the name of the function, with any additional inputs following in parentheses, separated by commas. a second time. For example, suppose a paragraph element has been assigned to the variable item. Then the font size can be extracted using item.style.fontSize.

Notice that the name of an attribute in CSS that has a hyphen, such as font-size, is written instead with the hyphen removed and the next letter capitalized, as in fontSize.

Method .getAttribute(attribute)

The methodA function associated with a class. getAttribute can be used to determine the value of a particular attribute, such as style, class, or id. Again assuming that a paragraph element has been assigned to the variable item, its ID can be extracted using item.getAttribute("id").