You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: en/document-object-model/README.md
+3-1Lines changed: 3 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,9 @@ description: The Document Object Model (DOM) is a programming interface that rep
7
7
8
8
# Document Object Model (DOM) {#dom}
9
9
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.
11
13
12
14
In this chapter, we will explore following topics:
Copy file name to clipboardExpand all lines: en/document-object-model/dom-api.md
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,7 @@ Considering a web page's content to be a tree of HTML elements is one way to _mo
15
15
16
16
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.
17
17
18
-
### Global Variables {-}
18
+
### Global Variables
19
19
20
20
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).
21
21
@@ -46,4 +46,4 @@ window.setTimeout(callback, 1000); //execute callback after an delay
46
46
window.setInterval(callback, 1000); //execute callback repeatedly after interval
47
47
```
48
48
49
-
<pclass="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—you should instead use in-window alerting options instead (such as showing a `<pclass="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
+
<pclass="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—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>
Copy file name to clipboardExpand all lines: en/document-object-model/dom-manipulation.md
+7-7Lines changed: 7 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ description: Understanding DOM Manipulation in JavaScript.
7
7
8
8
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—`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!
9
9
10
-
### Referencing HTML Elements {-}
10
+
### Referencing HTML Elements
11
11
12
12
In order to manipulate the DOM elements in a page, you first need to get a _reference_ to the element you want to change—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:
13
13
@@ -35,13 +35,13 @@ let elems = document.querySelectorAll(cssSelector);
35
35
36
36
- 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.
37
37
38
-
### Modifying HTML Elements {-}
38
+
### Modifying HTML Elements
39
39
40
40
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—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!
41
41
42
42
<pclass="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—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>
43
43
44
-
#### Changing Content {-}
44
+
#### Changing Content
45
45
46
46
You can use JavaScript to access and modify the **content** of a DOM element (e.g., the stuff between the start and close tags):
47
47
@@ -69,7 +69,7 @@ The `textContent` property of the element refers to _all_ of the content, but co
69
69
alertElem.textContent=""; //no more alert!
70
70
```
71
71
72
-
#### Changing Attributes {-}
72
+
#### Changing Attributes
73
73
74
74
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:
75
75
@@ -100,7 +100,7 @@ let isThick = document.querySelector("svg rect").hasAttribute("stroke-width"); /
100
100
101
101
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).
102
102
103
-
#### Changing Element CSS {-}
103
+
#### Changing Element CSS
104
104
105
105
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:
<pclass="alert alert-info">In general, you should modify element CSS by changing the class of the element, rather than specific style properties.</p>
142
142
143
-
### Modifying the DOM Tree {-}
143
+
### Modifying the DOM Tree
144
144
145
145
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!
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.
207
207
208
-
### Accessibility {-}
208
+
### Accessibility
209
209
210
210
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.
Copy file name to clipboardExpand all lines: en/document-object-model/listening-of-events.md
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -66,7 +66,7 @@ submitBtn.addEventListener("click", function (event) {
66
66
});
67
67
```
68
68
69
-
### Types of Events {-}
69
+
### Types of Events
70
70
71
71
There are [numerous different events](https://developer.mozilla.org/en-US/docs/Web/Events) that you can listen for, including:
72
72
@@ -109,7 +109,7 @@ There are [numerous different events](https://developer.mozilla.org/en-US/docs/W
109
109
110
110
<pclass="alert alert-warning">**Style guideline**: always register event listeners in the JavaScript—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>
111
111
112
-
### Event-Driven Programming {-}
112
+
### Event-Driven Programming
113
113
114
114
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:
0 commit comments