Ce Between DOM and BOM
Ce Between DOM and BOM
DOM :-
• The DOM stands for (Document Object Model)
• It represents the structure of a web page as a tree of objects (Node) that
can be manipulated with programming languages like JavaScript.
BOM :-
• The Browser Object Model (BOM) represents additional objects provided by
the browser for working with everything except the document.
• Key Features/E.g :-
• Window Object: The global object representing the browser window.
• Example: window.alert("Hello!");
• Navigator Object: Provides information about the browser.
• Example: navigator.userAgent (shows browser details).
• Screen Object: Gives information about the screen dimensions.
• Example: screen.width and screen.height.
Examples:-
Navigating Table :-
• The <table> Element :-
The <table> element has several specific properties to interact with its
content:
Where ,
• Live : All methods starts with "getElementsBy*" (Note :- Remember “s” in
Elements)return a live collection. Such collections always reflect the
current state of the document and “auto-update” when it changes.
• Static : Do not reflect changes to the DOM after the selection.
Closest :-
• The closest() method in JavaScript is used to find the nearest ancestor
(where, ancestor means: parent, the parent of parent, its parent and so ons ),
that matches a given CSS selector(including the element itself).
• Syntax :- element.closest(selector)
Where,
Element : - The starting point (the current element) from where the search for the
closest matching ancestor begins.
Selector :- A string representing a CSS selector to match the element (or ancestor)
against.
• Description :-
The closest() method starts from the element itself and traverses up the
DOM tree, looking for a matching ancestor (or the element itself) that
matches the specified selector. If no element matches the selector, it returns
null.
innerHTML :-
• innerHTML is a property of DOM elements in JavaScript that allows us to
get or set the HTML content inside an element. It includes not only the text
content but also any HTML tags within the element.
Description : -
• Get HTML Content: When used as a getter, innerHTML retrieves the
HTML markup of the element's content as a string.
• Set HTML Content: When used as a setter, innerHTML replaces the entire
content of the element with the provided string, which can include HTML.
textContent :-
• textContent is a property in JavaScript used to get or set the textual content
of a DOM element. It retrieves or modifies the text inside an element,
excluding any HTML tags, and does not parse the content as HTML.
• Please note that a standard attribute for one element can be unknown for
another one. For instance, "type" is standard for <input>
(HTMLInputElement), but not for <body> (HTMLBodyElement). Standard
attributes are described in the specification for the corresponding element
class.
• Here we can see it:
• Non-Standard Atribute:-
• If an attribute is non-standard, there won’t be a DOM-property for
it.So , All Non-Standard attributes are accessible by using the
following methods :
• elem.hasAttribute(name) : – checks for existence.
• elem.getAttribute(name) :– gets the value.
• elem.setAttribute(name, value) : – sets the value.
• elem.removeAttribute(name) :– removes the attribute.
elem.attributes:-
• The attributes property of an element (elem.attributes)
provides a collection of all the attributes of that element.
• This collection is an array-like object (not an array) that contains
“Attr “objects representing each attribute.
• E.g : -
Note :-
• Attributes – is what’s written in HTML.
• Properties – is what’s in DOM objects.
Creating an element :-
To create DOM nodes, there are two methods:
• document.createElement(tag) :-
• Creates a new element node with the given tag.
• E.g : -
• document.createTextNode(text) :-
• Creates a new text node with the given text:
• E.g : -
Insertion methods :-
• Insertion methods allow us to insert new HTML elements or text into a
webpage dynamically, at different positions relative to existing elements.
• There are multiple insertion method, which specify different places where we
can insert , that methods are :
• append
• prepend
• before
• after
• replaceWith
• insertAdjacentHTML:
• This method parses the specified text as HTML or XML and inserts it
into the DOM tree at a specified position.
• Syntax :-
• E.g:-
• insertAdjacentText :-
• This method inserts text content (not parsed as HTML) at a specified
position relative to the element
• Syntax:-
• E.g :-
• insertAdjacentElement :-
• This method inserts an element node at a specified position relative to
the element.
• Syntax :-
• E.g :-
Node Removal :-
• The node.remove() method is a simple way to remove a DOM node from its
parent.
• Syntax :-
• E.g :-
Note :-
• if we want to move an element to another place – there’s no need to remove
it from its old place.
• All insertion methods(append,prepend,...,etc) automatically remove the node
from the old place.
cloneNode():-
• The cloneNode() method is used to create a copy of a DOM node. You can
clone a node along with its descendants (deep clone) or without its
descendants (shallow clone).
• Syntax :-
• E.g:-
• Shallow Clone:-
• Deep Clone :-
• Complete E.g :-
• Point to remember :-
• Detached from Original Node: Cloned nodes do not inherit any
JavaScript event listeners or properties bound to the original node. You
need to reattach these manually.
• Attributes (like id, class, etc.) are copied to the cloned node, but
properties (like dataset or event listeners) are not.
• Use Cases:
• Duplicating elements for reuse (e.g., templating).
• Creating backup copies of nodes before modifying them.
• Implementing "Undo" functionality by storing the cloned nodes.
DocumentFragment() :-
• A DocumentFragment is a lightweight container used to hold multiple
elements before adding them to the DOM, improving performance.
• We can append other nodes to it, but when we insert it somewhere, then its
content is inserted instead.
• E.g ;-
getComputedStyle() :-
• The getComputedStyle() method is used to retrieve the computed styles of
an element as determined by the browser. This includes all CSS properties
applied to the element, whether they come from inline styles, external
stylesheets, or inherited rules.
• Syntax :-
getComputedStyle(element,[pseudoElement])
• E.g :-
• Getting a Single Computed Style:-
• Pseudo-Elements :-
We can retrieve the computed style of a pseudo-element :-
Event Propogation
• Event propagation is the mechanism by which an event travels through the
DOM tree when it is triggered. It describes the order in which event handlers
are called for nested elements.
• There are three main phases of event propagation:
• Capturing Phase (Event Capturing)
• Target Phase
• Bubbling Phase (Event Bubbling)
Event Delegation :-
• Event delegation is a technique in JavaScript that allows us to handle events
efficiently by taking advantage of event bubbling. Instead of adding event
listeners to multiple child elements, we attach a single event listener to their
parent and use it to manage events for all child elements.
• E.g : -