Document Object Model (DOM) Flashcards

Manipulating the web page's visual layout with JS via the Document Object Model

1
Q

What is the Browser JavaScript Interface to HTML Document, and how does it function?

A

The Browser JavaScript Interface to HTML Document is an interface that allows JavaScript to interact with and manipulate HTML documents within a web browser. It functions through a collection of objects and methods that enable JavaScript to query, update, and dynamically change the content and structure of web pages.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How does the Browser JavaScript Interface to HTML Document contribute to the interactivity of a web page?

A

This interface contributes to the interactivity of a web page by allowing JavaScript to dynamically manipulate the HTML document. This includes responding to user events, updating the content, changing styles, and modifying the document structure in real-time, thus making the web page responsive and interactive.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Define the Document Object Model (DOM) and its role in web development.

A

The Document Object Model (DOM) is a programming interface for web documents that represents the page as a tree of objects. Each object corresponds to a part of the document’s HTML. The DOM plays a crucial role in web development by enabling JavaScript to manipulate the HTML and CSS of a page, allowing for dynamic updates to content and style.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does the DOM enable a web page to be dynamic and responsive?

A

The DOM enables a web page to be dynamic and responsive by allowing JavaScript to access and manipulate the HTML and CSS elements of the page. Through the DOM, scripts can respond to user actions, alter page content, change styles, and more, thereby making the page react in real-time to user interactions and other events.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the DOM Hierarchy, and how is it structured?

A

The DOM Hierarchy is a tree-like structure in the Document Object Model that represents the HTML document. It is rooted at window.document, usually starting with the html tag, and follows the HTML document structure with branches like window.document.head and window.document.body. Each node in this hierarchy is a DOM object with various properties, most of which are private, but share common properties and methods as a DOM “Node”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain how the DOM hierarchy is useful in web development.

A

The DOM hierarchy is crucial in web development as it provides a structured and accessible way to interact with and manipulate the HTML document. By understanding this hierarchy, developers can efficiently traverse and modify the document, such as adding, removing, or altering elements, and responding to user events, thus creating dynamic and interactive web applications.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the key properties and methods of a DOM Node, and what are their functions?

A

Key properties of a DOM Node include nodeName, which identifies the element type or #text, and hierarchical properties like parentNode, nextSibling, previousSibling, firstChild, and lastChild. Key methods include getAttribute and setAttribute, which are used to access and modify node attributes. These properties and methods are essential for navigating and manipulating the DOM.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do the properties and methods of DOM Nodes facilitate web page manipulation?

A

The properties and methods of DOM Nodes facilitate web page manipulation by providing the means to identify, access, and modify elements within the DOM. For example, nodeName allows identification of node types, while properties like parentNode and nextSibling enable navigation within the DOM hierarchy. Methods like getAttribute and setAttribute allow for the alteration of node attributes, making it possible to dynamically update the content and appearance of web pages.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Describe the various methods of accessing DOM Nodes and highlight their differences.

A

DOM Nodes can be accessed in multiple ways. Walking the DOM hierarchy, though technically possible, is complex and fragile. More efficient methods include document.getElementById(), which accesses elements by their ID, and other methods like getElementsByClassName() and getElementsByTagName(), which retrieve multiple elements based on class name or tag name. These methods can simplify DOM manipulation and can start from any element in the document.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why is using methods like getElementById() preferred over walking the DOM hierarchy for accessing DOM Nodes?

A

Methods like getElementById() are preferred over walking the DOM hierarchy because they provide a more direct and reliable way to access elements. Walking the hierarchy is error-prone and can break with changes in the document structure. In contrast, getElementById() and similar methods offer a straightforward approach to locate elements with specific identifiers, making the process of accessing and manipulating DOM elements more efficient and less susceptible to errors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are textContent, innerHTML, outerHTML, getAttribute(), and setAttribute() in the context of DOM nodes?

A

In the context of DOM nodes, textContent returns the text content of a node and its descendants. innerHTML provides the HTML syntax describing the element’s descendants. outerHTML includes the HTML of the element itself along with its descendants. getAttribute() and setAttribute() are methods used to get and set the attributes of an element, respectively.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does innerHTML differ from outerHTML, and in what situations might each be used?

A

innerHTML and outerHTML differ in that innerHTML returns only the HTML syntax of an element’s descendants, whereas outerHTML returns the HTML syntax of the element itself along with its descendants. innerHTML is often used when the content within an element needs to be manipulated or retrieved, while outerHTML is useful when the entire element, including its tags, needs to be replaced or analyzed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Explain how DOM and CSS interactions work and the methods involved.

A

DOM and CSS interactions involve using JavaScript to dynamically update or query the styles and classes of DOM elements. Methods include updating an element’s class using element.className, modifying an element’s style (e.g., element.style.color), and querying the DOM using CSS selectors like document.querySelector() and document.querySelectorAll(). These methods allow for direct manipulation and selection of elements based on CSS styles.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Why is directly modifying an element’s style using JavaScript generally not preferred?

A

Directly modifying an element’s style using JavaScript, such as element.style.color, is generally not preferred because it can lead to harder-to-maintain code and potential conflicts with CSS stylesheets. It mixes content (HTML), presentation (CSS), and behavior (JavaScript), which goes against the principle of separation of concerns. Using classes to manage styles is more maintainable and allows for better separation and consistency in styling.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Describe the methods available for changing the structure of nodes in the DOM.

A

Methods for changing the node structure in the DOM include creating new elements using document.createElement() or document.createTextNode(), adding elements to the DOM using parent.appendChild() or parent.insertBefore(), and removing elements with node.removeChild(). Additionally, setting innerHTML is a common method for efficiently making structural changes to the DOM.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Why might setting innerHTML be preferred over other methods for changing the structure of nodes in the DOM?

A

Setting innerHTML is often preferred for changing the structure of nodes in the DOM because it is generally simpler and more efficient, especially for larger or more complex changes. It allows for the direct replacement or update of an element’s content with a single line of code, which can be more straightforward than creating, appending, or removing individual nodes. However, it should be used cautiously due to potential security risks like cross-site scripting (XSS).

17
Q

What are some additional operations that can be performed in the DOM?
(in addition to listening to events and updating the contents of the page)

A

Additional DOM operations include redirecting to a new page using window.location.href and communicating with the user. Communication can be done through the browser log with console.log(), or through popup dialogs with alert() and confirm(). Redirection can terminate JavaScript script execution.

18
Q

What should be considered when using window.location.href for page redirection in a JavaScript script?

A

When using window.location.href for page redirection, it is important to consider that this action may terminate the execution of the current JavaScript script. This means that any code following the redirection command may not be executed, which should be taken into account when designing the script’s flow and ensuring all necessary operations are completed before the redirection.

19
Q

Describe the DOM’s Coordinate System and how element positions are determined.

A

The DOM’s Coordinate System has its origin at the upper left, with y increasing downwards. The position of an element is determined by the upper-left outside corner of its margin. Positions can be read using element.offsetLeft and element.offsetTop, which are relative to element.offsetParent, not necessarily the same as element.parentNode.

20
Q

How do element.offsetLeft and element.offsetTop work, and what should be considered when using them?

A

element.offsetLeft and element.offsetTop provide the left and top positions of an element, respectively, in the DOM’s Coordinate System. They are relative to element.offsetParent, which is the nearest positioned ancestor element. It’s important to consider that these coordinates may not always be relative to the parent node, especially if the parent node is not positioned (i.e., does not have a CSS position other than “static”).

21
Q

Explain how elements can be positioned explicitly in the DOM.

A

Elements in the DOM can be positioned explicitly by changing their style to anything but “static”, such as “absolute”. This is done using CSS properties like element.style.position = “absolute”, and setting coordinates with element.style.left and element.style.top. When positioned absolutely, the element is removed from the normal document flow and does not occupy space.

22
Q

What happens to an element and its space in the document flow when it is positioned as “absolute”?

A

When an element is positioned as “absolute”, it is pulled out of the normal document flow, meaning it no longer occupies or affects the space originally taken up in the document. As a result, other elements in the document flow can move into the space that was occupied by the absolutely positioned element.

23
Q

What is a positioning context in the DOM, and how is it determined?

A

A positioning context in the DOM refers to the context within which an element’s position is determined relative to an ancestor element, known as offsetParent. This is typically the element, but can change if an element’s position is set to absolute or relative, in which case that element becomes the new offsetParent for its descendants.

24
Q

How does setting an element’s position to absolute or relative affect its descendants’ positioning context?

A

Setting an element’s position to absolute or relative creates a new positioning context for that element. As a result, it becomes the offsetParent for its descendants. This means the position of any child elements (using properties like style.left or style.top) will be calculated relative to this newly positioned element, rather than the default element or another offsetParent.

25
Q

How can the dimensions of an element in the DOM be read and updated?

A

The dimensions of an element in the DOM can be read using element.offsetWidth and element.offsetHeight, which include the content, padding, and border, but not the margin. To update an element’s dimensions, element.style.width and element.style.height are used.

26
Q

What do element.offsetWidth and element.offsetHeight include and exclude in their calculations?

A

element.offsetWidth and element.offsetHeight include the content, padding, and border of an element in their calculations, but they exclude the margin. This means they provide the total visible width and height of the element, without considering the space occupied by the margin.