How To Traverse The DOM In JavaScript?

DOM Traversal

  • parentNode – Returns parent node
  • parentElement – Returns parent element node
  • childNodes – Returns collection of an element’s child nodes (including text and comment nodes)
  • children – Returns a collection of an element’s child element node (do not include text and comment nodes)
  • firstChild – Returns first child node of an element (it can be text or comment node)
  • firstElementChild – Returns first child element node of an element
  • lastChild – Returns last child node of an element (it can be text or comment node)
  • lastElementChild – Returns last child element node of an element
  • previousSibling – Returns previous node of the same level (it can be text or comment node)
  • previousElementSibling – Returns previous element node of the same level
  • nextSibling – Returns next node of same level (it can be text or comment node)
  • nextElementSibling – Returns next element of same level

parentNode and parentElement

The parentNode read-only property returns the parent of the specified node in the DOM tree.

The parentElement property returns the parent element of the specified element. If the parent node is not an element node, it returns null.

childNodes

The childNodes read-only property returns a live NodeList of child nodes of the given element where the first child node is assigned index 0. childNodes includes all child nodes, including non-element nodes like text and comment nodes.

  • Whitespace inside elements is considered as text, and text is considered as nodes.
  • Any whitespace will create a #text node, from a single space to multiple spaces, returns, tabs, and so on.
  • You can use the length property of the NodeList object to determine the number of child nodes, then you can loop through all child nodes and extract the other info.

children

The children property returns collection of child elements of the given element where the first child is assigned index 0. children include only element nodes (no whitespace, text node, and comment node).

  • You can use the length property of the NodeList object to determine the number of child nodes, then you can loop through all child nodes and extract the other info.

firstChild

The firstChild property returns the node’s first child in the tree, or null if the node has no children. If the node is a Document, it returns the first node in the list of its direct children. It includes a text node and a comment node.

  • Whitespace inside elements is considered as text, and text is considered as nodes.
  • Any whitespace will create a #text node, from a single space to multiple spaces, returns, tabs, and so on.

firstElementChild

The firstElementChild property returns the element’s first child element node in the tree, or null if the node has no children element. It ignores text node and comment node or any whitespaces.

lastChild

The lastChild read-only property returns the last child of the node. If its parent is an element, then the child is generally an element node, a text node, or a comment node. It returns null if there are no child elements.

  • Whitespace inside elements is considered as text, and text is considered as nodes.
  • Any whitespace will create a #text node, from a single space to multiple spaces, returns, tabs, and so on.

lastElementChild

The lastElementChild property returns the element’s last child element node in the tree, or null if the node has no children element. It ignores text node and comment node or any whitespaces.

previousSibling

The previousSibling read-only property returns the node immediately preceding the specified one in its parent’s childNodes list, or null if the specified node is the first in that list. It includes text node and comment node or any whitespace.

previousElementSibling

The previousElementSibling property returns the element node immediately preceding the specified one in its parent’s node list, or null if the specified node is the first in that list. It ignores the text node and comment node or any whitespace.

nextSibling

The nextSibling read-only property returns the node immediately following the specified one in its parent’s childNodes list, or null if the specified node is the last node in that list. It includes text node and comment node or any whitespace.

nextElementSibling

The nextElementSibling read-only property returns the element node immediately following the specified one in its parent’s node list, or null if the specified node is the last node in that list. It ignores the text node and comment node or any whitespace.

ownerDocument

The ownerDocument read-only property returns the top-level document object for this node. In HTML, the HTML document itself is always the ownerDocument of an element.

Tagged : / / /

Most Popular Visual Studio Code Shortcut

ShortcutDescription
Ctrl-SSaves the currently selected file.
Ctrl-PDisplays the Print dialog.
Ctrl+ZUndo
Ctrl+YRedo
Ctrl+]Indent Line
Ctrl+[Outdent Line
Ctrl-Shift-SSaves all files and projects.
Shift-F7Switches from the code view to the design view in the editor.
F7Switches from the design view to the code view in the editor.
Shift-F8 or F8Navigate to compile-time errors.
Ctrl + B Toggle Slider Bar.
Ctrl + JToggle Panels.
Ctrl + Shift + DDebug folder or file.
Ctrl + Shift + FSearch in all files.
Ctrl + Shift + HReplace in all the files.
Ctrl + Shift + XGoto Extension Window
Ctrl + Shift + U Show output Window
Ctrl + 1 Focus Left Editor.
Ctrl+ 2Focus Right Editor.
Shift+Alt+DownCopy Line Down.
Shift+Alt+UpCopy Line Up.
Ctrl+Shift+\Jump to matching bracket.
HomeGo to Beginning of Line.
EndGo to End of Line.
Ctrl+EndGo to End of File.
Ctrl+HomeGo to Beginning of File.
Ctrl+DownScroll Line Down.
Ctrl+UpScroll Line Up.
Ctrl+K Ctrl+CAdd Line Comment.
Tagged : / / /

HTML: Object tag

The <object>…..</object> tag is used to embedded object within HTML document.

Ex: – audio, video, Java applets, ActiveX, PDF, and Flash etc.

<object> tag also used to embed another webpage into a HTML document.

The text between the <object> and </object> is an alternate text, for browsers that do not support this tag.

<param> tag is used to pass parameters to plugins that have been embedded with the <object> tag.

AttributeValueDescription
DataURLSpecifies the URL of the resource to be used by the object
TypeMedia typeSpecifies the media type of data specified in the data attribute
NameNameSpecifies a name for the object
HeightPixelsSpecifies the height of the object
WidthPixelsSpecifies the width of the object
Usemap#mapnameSpecifies the name of a client-side image map to be used with the object
FormForm_idSpecifies one or more forms the object belongs to
Tagged : / / /

HTML: Embed Tag

The <embed> tag is used to embed an audio file or video file on the web page. The <embed> tag defines a container for an external application or interactive content (a plug-in).

The <embed> the tag defines a container for an external resource, such as a web page, a picture, a media player, or a plug-in application.

AttributeValueDescription
srcURLSpecifies the path of the file name to play an audio or video file
typeMedia typeSpecifies the media type of the linked document
HeightPixelsSpecifies the height of the embedded content
WidthPixelsSpecifies the width of the embedded content

Noembed Tag:

The <noembed>…..</noembed> tag is used to handle browsers that do not support the <embed> tag. <noembed> tag specifies a message that you want to display in the browser. The message inside the <noembed> tag appears only when the browser doesn’t support the <embed> tag.

#This tag has been deprecated, should not be used in production.

Tagged : / / /