Mixin ParentNode

This mixing is designed to handle parent elements (ancestors), i.e. Elements containing one and more descendant (child elements).

  • children — descendants of the element

Such a structure is called the HTML collection and is an arms-like object (pseudoarray). There is another similar structure — list of Nodes(NodeList).

Array-like objects have the length property with the number of descendants, the forEach() method, (NodeList), which allows you to move the nodes (to make iteration). Such objects allow to obtain elements by index, by name (HTMLCollection), etc. However, they have no methods of real arrays, such as map(), filter(), reduce() etc., which makes work with them not very comfortable. Therefore, array objects are recommended to convert to arrays using the Array.from() or Spread operator method:

  • firstElementChild — first descendant — element
  • lastElementChild — the last descendant — element

For further manipulations, we will need to periodically create new elements, so we will create another utility:

Our utility accepts 4 arguments: identifier, text, tag name and CSS class. 2 arguments (tag and class) have default values. The function returns an element ready for operation. Subsequently, we implement a more universal version of this utility.

  • prepend(newNode) — adds an item to the top of the list
  • append(newNode) — adds an element to the end of the list

One of the interesting features HTMLCollection is that it is “alive”, i.e. The elements returned by reference and their number are updated automatically. However, this feature cannot be used, for example, to automatically add event handlers.

  • replaceChildren(nodes) — replaces descendants with new elements

The most versatile methods for obtaining references to elements are querySelector(selector)and querySelectorAll(selector)methods. Moreover, unlike getElementById(), they can be called on any parent element, and not just on document. Any valid CSS selector is transmitted as an argument to these methods. (id, class, tag, etc):

Create a universal utility to receive items:

Our utility takes 3 arguments: CSS selector, parent element and indicator of the number of elements (one or all). 2 arguments (ancestor and indicator) have default values. The function returns either one or all items (as a conventional array), which coincide with the selector, depending on the indicator value:

Mixin NonDocumentTypeChildNode

This mixing is designed to process subsidiaries that are not a document, i.e. all nodes except document.

  • previousElementSibling — previous element
  • nextElementSibling —next element

Mixin ChildNode

This mixing is designed to process subsidiaries, i.e. elements that are descendants of other elements.

  • before(newNode) — inserts a new element before the current
  • after(newNode) — inserts a new element after the current
  • replaceWith(newNode) —replaces the current element of the new
  • remove() — removes the current element

Node Interface

This interface is designed to process nodes.

  • nodeType — type of node
  • nodeName — name of node
  • baseURI — main path
  • parentNode — parent node
  • parentElement —parent element
  • hasChildNodes() — returns true, If the element has at least one descendant
  • childNodes — child nodes
  • firstChild — the first node-child
  • lastChild —the last node-child
  • nextSibling — next node
  • previousSibling — previous node
  • textContent — getter / setter for extract / recording text

To extract / write text there is another (outdated) getter / setter— innerText.

  • cloneNode(deep) — copies the node. Accepts a logical value determining the nature of copying: superficial — only the node itself is copied, the assembly itself and all its descendants is copied
  • isEqualNode(node) — compares nodes
  • isSameNode(node) — determines the identity of the nodes
  • contains(node) — returns true, if the element contains the specified node
  • insertBefore(newNode, existingNode) — adds a new node(newNode) before existing (existingNode)
  • appendChild(node) — adds a node to the end of the list
  • replaceChild(newNode, existingNode) — replaces an existing node (existingNode) with a new(newNode):
  • removeChild(node) — removes the specified child node

Document interface

This interface is designed to process theDocument object.

  • URL and documentURI — document address
  • documentElement:
  • getElementsByTagName(tag) — returns all elements with the specified tag
  • getElementsByClassName(className) — returns all items with the specified CSS class
  • createDocumentFragment() — returns a fragment of the document:

Fragments allow you to avoid creating unnecessary elements. They are often used when working with markup, hidden from the user using thetemplate tag (method cloneNode() returns DocumentFragment).

  • createTextNode(data) —create text
  • createComment(data) — create comment
  • importNode(existingNode, deep) — creates a new node based on existing
  • createAttribute(attr) — creates the specified attribute

NodeIterator and TreeWalker interfaces

NodeIterator and TreeWalker interfaces are designed to bypass (traverse) node trees. I did not come across the examples of their practical use, so I will limit the pair of examples:

Element interface

This interface is intended for processing elements.

  • localName and tagName — a tag name
  • id — getter / setter for Identifier
  • className — getter / setter for CSS-class
  • classList — all CSS-classes of element(DOMTokenList object)

--

--

Evgenii Kononov
Geek Culture

MERN Developer | Full Stack Engineer | Front End Specialist | HTML5, CSS3, React, NodeJS, ExpressJS, MongoDB