| OLD | NEW |
| 1 --- | 1 --- |
| 2 layout: default | 2 layout: default |
| 3 title: "Add Elements to the DOM" | 3 title: "Add Elements to the DOM" |
| 4 description: "You have an Element object, now what?" | 4 description: "You have an Element object, now what?" |
| 5 has-permalinks: true | 5 has-permalinks: true |
| 6 tutorial: | 6 tutorial: |
| 7 id: add-elements | 7 id: add-elements |
| 8 next: remove-elements | 8 next: remove-elements/ |
| 9 next-title: "Remove DOM Elements" | 9 next-title: "Remove DOM Elements" |
| 10 prev: connect-dart-html | 10 prev: connect-dart-html/ |
| 11 prev-title: "Connect Dart & HTML" | 11 prev-title: "Connect Dart & HTML" |
| 12 --- | 12 --- |
| 13 | 13 |
| 14 {% capture whats_the_point %} | 14 {% capture whats_the_point %} |
| 15 | 15 |
| 16 * In Dart, page elements are of type Element. | 16 * In Dart, page elements are of type Element. |
| 17 * An Element knows its parent. | 17 * An Element knows its parent. |
| 18 * An Element keeps its children in a List\<Element>. | 18 * An Element keeps its children in a List\<Element>. |
| 19 * Change the DOM by adding or removing children of elements. | 19 * Change the DOM by adding or removing children of elements. |
| 20 * Respond to user input with an EventListener. | 20 * Respond to user input with an EventListener. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 46 <h3>Dynamically add items to the browser page.</h3> | 46 <h3>Dynamically add items to the browser page.</h3> |
| 47 </div> | 47 </div> |
| 48 | 48 |
| 49 As you learned in the previous target, | 49 As you learned in the previous target, |
| 50 the DOM represents the structure | 50 the DOM represents the structure |
| 51 of a web page document using a simple tree structure. | 51 of a web page document using a simple tree structure. |
| 52 Each node in the tree represents an item on the page. | 52 Each node in the tree represents an item on the page. |
| 53 Each node in the tree keeps track of both | 53 Each node in the tree keeps track of both |
| 54 its parent and its children. | 54 its parent and its children. |
| 55 In Dart, the | 55 In Dart, the |
| 56 <a href="http://api.dartlang.org/dart_html/Node.html" target="_blank">Node</a> | 56 <a href="https://api.dartlang.org/dart_html/Node.html" target="_blank">Node</a> |
| 57 class contains the methods and properties | 57 class contains the methods and properties |
| 58 that implement a node's tree functionality. | 58 that implement a node's tree functionality. |
| 59 | 59 |
| 60 HTML page elements are one kind of node that can be in the DOM tree. | 60 HTML page elements are one kind of node that can be in the DOM tree. |
| 61 They have a rectangular area on the page and can receive events. | 61 They have a rectangular area on the page and can receive events. |
| 62 Examples of elements include | 62 Examples of elements include |
| 63 heading elements, paragraph elements, table elements, | 63 heading elements, paragraph elements, table elements, |
| 64 button elements, and so on. | 64 button elements, and so on. |
| 65 | 65 |
| 66 In Dart, | 66 In Dart, |
| 67 elements are implemented by the | 67 elements are implemented by the |
| 68 <a href="http://api.dartlang.org/dart_html/Element.html" target="_blank">Element
</a> | 68 <a href="https://api.dartlang.org/dart_html/Element.html" target="_blank">Elemen
t</a> |
| 69 class, which is a subclass of Node. | 69 class, which is a subclass of Node. |
| 70 Because the nodes you care about most are usually elements, | 70 Because the nodes you care about most are usually elements, |
| 71 this target focuses on Element, | 71 this target focuses on Element, |
| 72 rather than on Node. | 72 rather than on Node. |
| 73 | 73 |
| 74 * [Copy and run the todo app](#copy-app) | 74 * [Copy and run the todo app](#copy-app) |
| 75 * [About parent and child Elements in Dart](#tree-structure) | 75 * [About parent and child Elements in Dart](#tree-structure) |
| 76 * [Setting up the page in HTML](#html-code) | 76 * [Setting up the page in HTML](#html-code) |
| 77 * [Getting an element from the DOM](#dart-code) | 77 * [Getting an element from the DOM](#dart-code) |
| 78 * [Registering an event handler](#event-handler) | 78 * [Registering an event handler](#event-handler) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 89 This program dynamically changes the DOM, | 89 This program dynamically changes the DOM, |
| 90 and therefore the web page, | 90 and therefore the web page, |
| 91 by adding elements to the DOM tree. | 91 by adding elements to the DOM tree. |
| 92 | 92 |
| 93 **Try it!** Type in the text field and press return. | 93 **Try it!** Type in the text field and press return. |
| 94 The app adds an item to the list. | 94 The app adds an item to the list. |
| 95 Enter a few items into the input field: | 95 Enter a few items into the input field: |
| 96 | 96 |
| 97 <iframe class="running-app-frame" | 97 <iframe class="running-app-frame" |
| 98 style="height:250px;width:300px;" | 98 style="height:250px;width:300px;" |
| 99 src="http://dart-lang.github.com/dart-tutorials-samples/web/target03/tod
o/web/todo.html"> | 99 src="http://dart-lang.github.io/dart-tutorials-samples/web/target03/todo
/web/todo.html"> |
| 100 </iframe> | 100 </iframe> |
| 101 | 101 |
| 102 You can find the complete source code for this sample on github at | 102 You can find the complete source code for this sample on github at |
| 103 <a href="https://github.com/dart-lang/dart-tutorials-samples/tree/master/web/tar
get03/todo" | 103 <a href="https://github.com/dart-lang/dart-tutorials-samples/tree/master/web/tar
get03/todo" |
| 104 target="_blank">todo</a>. | 104 target="_blank">todo</a>. |
| 105 | 105 |
| 106 This is the beginning of an app to manage a list of things to do. | 106 This is the beginning of an app to manage a list of things to do. |
| 107 Right now, this app is for procrastinators only | 107 Right now, this app is for procrastinators only |
| 108 because the program can only add items to your to do list | 108 because the program can only add items to your to do list |
| 109 but not remove them. | 109 but not remove them. |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 is in the top-level variable `toDoList`. | 227 is in the top-level variable `toDoList`. |
| 228 | 228 |
| 229 Note the types of these variables: InputElement and UListElement. | 229 Note the types of these variables: InputElement and UListElement. |
| 230 These are both subclasses of Element. | 230 These are both subclasses of Element. |
| 231 The dart:html library has dozens of Element subclasses, | 231 The dart:html library has dozens of Element subclasses, |
| 232 many of which correspond to certain HTML tags. | 232 many of which correspond to certain HTML tags. |
| 233 This program uses three: | 233 This program uses three: |
| 234 | 234 |
| 235 | HTML tag | Dart class | | 235 | HTML tag | Dart class | |
| 236 |---|---| | 236 |---|---| |
| 237 | \<input> | <a href="http://api.dartlang.org/dart_html/InputElement.html" targe
t="_blank">InputElement</a> | | 237 | \<input> | <a href="https://api.dartlang.org/dart_html/InputElement.html" targ
et="_blank">InputElement</a> | |
| 238 | \<ul> | <a href="http://api.dartlang.org/dart_html/UListElement.html" target="
_blank">UListElement</a> | | 238 | \<ul> | <a href="https://api.dartlang.org/dart_html/UListElement.html" target=
"_blank">UListElement</a> | |
| 239 | \<li> | <a href="http://api.dartlang.org/dart_html/LIElement.html" target="_bl
ank">LIElement</a> | | 239 | \<li> | <a href="https://api.dartlang.org/dart_html/LIElement.html" target="_b
lank">LIElement</a> | |
| 240 {: .table} | 240 {: .table} |
| 241 | 241 |
| 242 ## Registering an event handler {#event-handler} | 242 ## Registering an event handler {#event-handler} |
| 243 | 243 |
| 244 When a user enters text into the input field, | 244 When a user enters text into the input field, |
| 245 a _change_ event fires, | 245 a _change_ event fires, |
| 246 indicating that the value in the input field has just changed. | 246 indicating that the value in the input field has just changed. |
| 247 The todo app defines a function, addToDoItem(), | 247 The todo app defines a function, addToDoItem(), |
| 248 that can handle these change events. | 248 that can handle these change events. |
| 249 The following code connects addToDoItem() to the input field: | 249 The following code connects addToDoItem() to the input field: |
| 250 | 250 |
| 251  | 251  |
| 252 | 252 |
| 253 Rather than dissect this busy line of code, | 253 Rather than dissect this busy line of code, |
| 254 think of it as a Dart idiom | 254 think of it as a Dart idiom |
| 255 for adding an event handler to an Element. | 255 for adding an event handler to an Element. |
| 256 | 256 |
| 257  | 257  |
| 258 | 258 |
| 259 A change event is just one of many different types of events | 259 A change event is just one of many different types of events |
| 260 that an input element can generate. | 260 that an input element can generate. |
| 261 For example, you can use `click` to handle mouse clicks, | 261 For example, you can use `click` to handle mouse clicks, |
| 262 or `keyDown` for when the user presses a key on the keyboard. | 262 or `keyDown` for when the user presses a key on the keyboard. |
| 263 | 263 |
| 264 ##About EventListener functions {#about-event-listeners} | 264 ##About EventListener functions {#about-event-listeners} |
| 265 | 265 |
| 266 The argument passed to the listen() method is a _callback function_ | 266 The argument passed to the listen() method is a _callback function_ |
| 267 of type | 267 of type |
| 268 <a href="http://api.dartlang.org/dart_html/EventListener.html" target="_blank">E
ventListener</a>. | 268 <a href="https://api.dartlang.org/dart_html/EventListener.html" target="_blank">
EventListener</a>. |
| 269 EventListener is a typedef defined in the dart:html library as follows: | 269 EventListener is a typedef defined in the dart:html library as follows: |
| 270 | 270 |
| 271 {% prettify dart %} | 271 {% prettify dart %} |
| 272 typedef void EventListener(Event event) | 272 typedef void EventListener(Event event) |
| 273 {% endprettify %} | 273 {% endprettify %} |
| 274 | 274 |
| 275 As you can see, an EventListener returns no value (void) and takes an | 275 As you can see, an EventListener returns no value (void) and takes an |
| 276 <a href="http://api.dartlang.org/dart_html/Event.html" target="_blank">Event</a> | 276 <a href="https://api.dartlang.org/dart_html/Event.html" target="_blank">Event</a
> |
| 277 object as an argument. | 277 object as an argument. |
| 278 Any function with this signature is an EventListener. | 278 Any function with this signature is an EventListener. |
| 279 Based on its signature, the addToDoItem() function is an EventListener. | 279 Based on its signature, the addToDoItem() function is an EventListener. |
| 280 | 280 |
| 281 {% prettify dart %} | 281 {% prettify dart %} |
| 282 void addToDoItem(Event e) { ... } | 282 void addToDoItem(Event e) { ... } |
| 283 {% endprettify %} | 283 {% endprettify %} |
| 284 | 284 |
| 285 The Event object passed into an EventListener function | 285 The Event object passed into an EventListener function |
| 286 carries information about the Event that occurred. | 286 carries information about the Event that occurred. |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 element with the ID to-do-list, thus styling | 328 element with the ID to-do-list, thus styling |
| 329 each item in the to do list. | 329 each item in the to do list. |
| 330 | 330 |
| 331 ##Moving elements within the DOM tree {#moving-elements} | 331 ##Moving elements within the DOM tree {#moving-elements} |
| 332 | 332 |
| 333 Here's an example that shows how to move an element within the DOM. | 333 Here's an example that shows how to move an element within the DOM. |
| 334 **Try it!** Form a word by clicking the letter tiles. | 334 **Try it!** Form a word by clicking the letter tiles. |
| 335 | 335 |
| 336 <iframe class="running-app-frame" | 336 <iframe class="running-app-frame" |
| 337 style="height:400px;width:400px;" | 337 style="height:400px;width:400px;" |
| 338 src="http://dart-lang.github.com/dart-tutorials-samples/web/target03/ana
gram/web/anagram.html"> | 338 src="http://dart-lang.github.io/dart-tutorials-samples/web/target03/anag
ram/web/anagram.html"> |
| 339 </iframe> | 339 </iframe> |
| 340 | 340 |
| 341 You can find the complete source code for this sample on github at | 341 You can find the complete source code for this sample on github at |
| 342 <a href="https://github.com/dart-lang/dart-tutorials-samples/tree/master/web/tar
get03/anagram" | 342 <a href="https://github.com/dart-lang/dart-tutorials-samples/tree/master/web/tar
get03/anagram" |
| 343 target="_blank">anagram</a>. | 343 target="_blank">anagram</a>. |
| 344 | 344 |
| 345 When the program starts, | 345 When the program starts, |
| 346 it creates one button element for each of seven | 346 it creates one button element for each of seven |
| 347 randomly selected letters. | 347 randomly selected letters. |
| 348 The program adds each button to a DOM element—a simple | 348 The program adds each button to a DOM element—a simple |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 You can find more information about the DOM and CSS in | 384 You can find more information about the DOM and CSS in |
| 385 <a href="/docs/dart-up-and-running/">Dart Up and Running</a>, | 385 <a href="/docs/dart-up-and-running/">Dart Up and Running</a>, |
| 386 which also provides thorough coverage of the Dart language, | 386 which also provides thorough coverage of the Dart language, |
| 387 libraries, and tools. | 387 libraries, and tools. |
| 388 </li> | 388 </li> |
| 389 </ul> | 389 </ul> |
| 390 | 390 |
| 391 {% endcapture %} | 391 {% endcapture %} |
| 392 | 392 |
| 393 {% include tutorial.html %} | 393 {% include tutorial.html %} |
| OLD | NEW |