Skip to content

Commit f59075f

Browse files
committed
added dom chapter
1 parent e690d31 commit f59075f

File tree

6 files changed

+26
-20
lines changed

6 files changed

+26
-20
lines changed

en/SUMMARY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@
152152
- [Execution Context](behind-scenes/execution-context.md)
153153
- [Memory Heap](behind-scenes/memory-heap.md)
154154
- [Runtime Environment](behind-scenes/runtime-environment.md)
155+
- [Document Object Model (DOM)](document-object-model/README.md)
156+
- [DOM API](document-object-model/dom-api.md)
157+
- [DOM Manipulation](document-object-model/dom-manipulation.md)
158+
- [Listening for Events](document-object-model/listening-of-events.md)
155159
- [References](References.md)
156160
- [Resources](resources.md)
157161
- [Credits](Credits.md)

en/document-object-model/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ description: The Document Object Model (DOM) is a programming interface that rep
77

88
# Document Object Model (DOM) {#dom}
99

10-
The primary purpose of using JavaScript in a web page is to make that page **interactive**: the JavaScript language is used to program logical decisions that will effect what is shown on the page. It does this primarily by _changing the HTML rendered by the browser_. For example, JavaScript can be used to change the text inside a `<p>`, add addition `<li>` elements to a list, or to give a `<div>` a new CSS `class` attribute. The programmatic representation of the HTML elements currently being shown by the browser is known as the **Document Object Model (DOM)**. In web programming JavaScript code is used to modify the DOM (HTML elements currently being shown by the browser) in response to user input, thereby making the page interactive. This chapter introduces the Document Object Model and how to use JavaScript to manipulate it through user-driven interaction.
10+
The primary purpose of using JavaScript in a web page is to make that page **interactive**: the JavaScript language is used to program logical decisions that will effect what is shown on the page. It does this primarily by _changing the HTML rendered by the browser_. For example, JavaScript can be used to change the text inside a `<p>`, add addition `<li>` elements to a list, or to give a `<div>` a new CSS `class` attribute.
11+
12+
The programmatic representation of the HTML elements currently being shown by the browser is known as the **Document Object Model (DOM)**. In web programming JavaScript code is used to modify the DOM (HTML elements currently being shown by the browser) in response to user input, thereby making the page interactive. This chapter introduces the Document Object Model and how to use JavaScript to manipulate it through user-driven interaction.
1113

1214
In this chapter, we will explore following topics:
1315
* [DOM API](./dom-api.md)

en/document-object-model/dom-api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Considering a web page's content to be a tree of HTML elements is one way to _mo
1515

1616
Moreover, the DOM also provides an **Application Programming Interface (API)** which allows computer _applications_ to _programmatically_ (e.g., through JavaScript code) _interact_ with it: accessing and manipulating the tree of elements. An API is often a set of _functions_ and _variables_ that can be used give instructions to a program. The DOM API is no different: it is a group of functions you can call and variables (usually Object properties) you can adjust to change the rendered web content. You write code to call these functions in order to make a page interactive.
1717

18-
### Global Variables {-}
18+
### Global Variables
1919

2020
You can programmatically access the API in JavaScript by utilizing a set of global variables. **Global variables** are variables that are "globally" scoped: they are available anywhere in the program (not just within a particular function).
2121

@@ -46,4 +46,4 @@ window.setTimeout(callback, 1000); //execute callback after an delay
4646
window.setInterval(callback, 1000); //execute callback repeatedly after interval
4747
```
4848

49-
<p class="alert alert-warning">While these examples are included for completeness, most `window` functions are rarely used and should be avoided. Popups with the `window.alert()` function are inelegant, interrupt the user's actions, and produce a bad user experience&mdash;you should instead use in-window alerting options instead (such as showing a `<p class="alert">`). Browser control functions such as `scrollTo()` are non-standard and can vary drastically across systems and platforms. Proceed with caution when using `window` functions!</p>
49+
<p class="alert alert-warning">While these examples are included for completeness, most `window` functions are rarely used and should be avoided. Popups with the `window.alert()` function are inelegant, interrupt the user's actions, and produce a bad user experience&mdash;you should instead use in-window alerting options instead (such as showing a `class="alert-class" `). Browser control functions such as `scrollTo()` are non-standard and can vary drastically across systems and platforms. Proceed with caution when using `window` functions!</p>

en/document-object-model/dom-manipulation.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ description: Understanding DOM Manipulation in JavaScript.
77

88
While `window` represents the Browser, the **DOM** itself is represented by the [**`document`**](https://developer.mozilla.org/en-US/docs/Web/API/Document) global object&mdash;`document` _is_ the DOM (the _current_ HTML rendered in the browser). You access properties and call methods of this object in order to manipulate the content displayed in the browser!
99

10-
### Referencing HTML Elements {-}
10+
### Referencing HTML Elements
1111

1212
In order to manipulate the DOM elements in a page, you first need to get a _reference_ to the element you want to change&mdash;that is, you need a variable that refers to that element. You can get these variable references by using one of the `document` "selector" functions:
1313

@@ -35,13 +35,13 @@ let elems = document.querySelectorAll(cssSelector);
3535

3636
- Note that the methods that return multiple nodes (e.g., `querySelectorAll`) return a [`NodeList`](https://developer.mozilla.org/en-US/docs/Web/API/NodeList) object. While this is like an array (you can access elements via index through bracket notation and it has a `.length` property), it is **not** an array: meaning it doesn't support methods like `forEach()` and `map()` across all browsers. If you need to iterate through a `NodeList`, you should use a regular `for` loop. But in practice, you're much more likely to only work with single elements at a time.
3737

38-
### Modifying HTML Elements {-}
38+
### Modifying HTML Elements
3939

4040
Once you have a reference to an element, you access properties and call methods on that object in order to modify its state in the DOM&mdash;which will in turn modify how it _currently_ is displayed on the page. Thus by modifying these objects, you are dynamically changing the web page's content!
4141

4242
<p class="alert alert-warning">**Important**: setting these properties do not change the `.html` source code file! Instead, they just change the _rendered DOM elements_ (think: the content stored in the computer's memory rather than in a file). If you refresh the page, the content will go back to how the `.html` source code file specifies it should appear&mdash;unless that also loads the script that modifies the DOM. What is shown on the page is the HTML with the JavaScript modifications added in.</p>
4343

44-
#### Changing Content {-}
44+
#### Changing Content
4545

4646
You can use JavaScript to access and modify the **content** of a DOM element (e.g., the stuff between the start and close tags):
4747

@@ -69,7 +69,7 @@ The `textContent` property of the element refers to _all_ of the content, but co
6969
alertElem.textContent = ""; //no more alert!
7070
```
7171

72-
#### Changing Attributes {-}
72+
#### Changing Attributes
7373

7474
You can also change the **attributes** of individual elements. Each attribute defined in the HTML specification is typically exposed as a _property_ of the element object:
7575

@@ -100,7 +100,7 @@ let isThick = document.querySelector("svg rect").hasAttribute("stroke-width"); /
100100

101101
These methods will let you interact with attributes that are _not_ defined by the HTML spec, such as `data-` attribute. However, they _don't_ work with certain element attributes (such as the `value` attribute of an `<input>` element). Other elements may have their own special DOM properties: see the [DOM Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model) for a list of [HTML interfaces](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model#HTML_interfaces).
102102

103-
#### Changing Element CSS {-}
103+
#### Changing Element CSS
104104

105105
It is possible to modify the **CSS classes** (and even inline styling) of an element. But rather than using the `class` property like with other attributes, you instead access the **`classList`** property. On modern browsers (IE 10 or later), this property supports methods `.add()` and `.remove()` for adding and removing classes from the list:
106106

@@ -140,7 +140,7 @@ h1.style.backgroundColor = "black"; //not `.background-color`
140140

141141
<p class="alert alert-info">In general, you should modify element CSS by changing the class of the element, rather than specific style properties.</p>
142142

143-
### Modifying the DOM Tree {-}
143+
### Modifying the DOM Tree
144144

145145
In addition to modifying the individual DOM elements, it is also possible to access and modify the _DOM tree itself!_ That is, you can create new elements and add them to the tree (read: webpage), remove elements from the tree, or pluck them out of the tree and insert them somewhere else!
146146

@@ -205,7 +205,7 @@ main.removeChild(main.querySelector("p"));
205205

206206
The `appendChild()` method is considered a cleaner approach than just modifying the `innerHTML` property, as it allows you to adjust the DOM tree without erasing what was previously there. A common practice is to use `document.createElement()` to create a _block_ element, then set the `innerHTML` of that element to its content (which can include _inline_ elements), and then use `appendChild` to add the new block element to the tree at the desired location.
207207

208-
### Accessibility {-}
208+
### Accessibility
209209

210210
Whenever you learn a new technology, you should ask: **how does this affect accessibility?** With the JavaScript code modifying the rendered DOM, it is possible that the content of a page will change _after_ it has been read by a screen reader. And while a sighted user will likely be able to see the change visually, a screen reader has no way of knowing that something on the page is different unless you tell it.
211211

en/document-object-model/listening-of-events.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ submitBtn.addEventListener("click", function (event) {
6666
});
6767
```
6868

69-
### Types of Events {-}
69+
### Types of Events
7070

7171
There are [numerous different events](https://developer.mozilla.org/en-US/docs/Web/Events) that you can listen for, including:
7272

@@ -109,7 +109,7 @@ There are [numerous different events](https://developer.mozilla.org/en-US/docs/W
109109

110110
<p class="alert alert-warning">**Style guideline**: always register event listeners in the JavaScript&mdash;do _not_ utilize the HTML attributes such as `onclick`. This is to help keep concerns separated: the HTML should not need to know anything about the JavaScript that is utilized (since the browser may not even support JavaScript!), but it's okay for the JavaScript to rely on and modify the HTML.</p>
111111

112-
### Event-Driven Programming {-}
112+
### Event-Driven Programming
113113

114114
In a typical web program event callback functions can occur repeatedly, over and over again (e.g., every time the user clicks a button). This makes them potentially act a bit like the body of a `while` loop. However, because these callbacks are _functions_, any variables defined within them are **scoped** to that function, and will not be available on subsequent executions. Thus if you want to keep track of some additional information (e.g., how many times the button was clicked), you will need to use a variable declared _outside_ of the function (e.g., a **global** ). Such variables can be used to represent the **state** (situation) of the program, which can then be used to determine what behavior to perform when an event occurs, following the below pattern:
115115

en/resources.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@ chapter:
66
## Articles for learning JavaScript
77

88

9-
1. Introduction to JavaScript by Hostinger [Link](https://www.hostinger.com/tutorials/what-is-javascript)
9+
1. Document Object Model reference [Link](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model)
1010

11-
2. Introduction to JavaScript by Microverse [Link](https://www.microverse.org/blog/introduction-to-javascript-a-guide-for-beginners)
11+
2. Introduction to Events [Link](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events)
1212

13-
3. JavaScript Basics by Mozilla Developer [Link](https://developer.mozilla.org/en-US/docs/Learn/Getting_started_with_the_web/JavaScript_basics)
13+
3. Introduction to JavaScript by Hostinger [Link](https://www.hostinger.com/tutorials/what-is-javascript)
1414

15-
4. JavaScript Tutorial by Geeks for Geeks [Link](https://www.geeksforgeeks.org/javascript/)
15+
4. Introduction to JavaScript by Microverse [Link](https://www.microverse.org/blog/introduction-to-javascript-a-guide-for-beginners)
1616

17-
5. JavaScript Tutorial by Stackify [Link]( https://stackify.com/learn-javascript-tutorials/)
17+
5. JavaScript Basics by Mozilla Developer [Link](https://developer.mozilla.org/en-US/docs/Learn/Getting_started_with_the_web/JavaScript_basics)
1818

19-
6. What is the DOM? (CSS-Tricks) [Link](https://css-tricks.com/dom/)
19+
6. JavaScript Tutorial by Geeks for Geeks [Link](https://www.geeksforgeeks.org/javascript/)
2020

21-
7. Document Object Model reference [Link](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model)
21+
7. JavaScript Tutorial by Stackify [Link]( https://stackify.com/learn-javascript-tutorials/)
2222

2323
8. JavaScript HTML DOM reference [Link](https://www.w3schools.com/js/js_htmldom.asp)
2424

25-
9. Introduction to Events [Link](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events)
25+
9. What is the DOM? (CSS-Tricks) [Link](https://css-tricks.com/dom/)
2626

2727
## Books for learning JavaScript
2828

0 commit comments

Comments
 (0)