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: js/lesson3/tutorial.md
+25-19Lines changed: 25 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,10 +24,10 @@ jQuery is a JavaScript library that supplies you with functionality independent
24
24
25
25
Selectors are simplified in jQuery. You can access elements by element type, id or class, just like in `CSS`.
26
26
27
-
For example, to retrieve all paragraph elements you can use this selector `$("p")` or to retrieve all elements with a specific id `$("#message")` instead of using `getElementByTag("p")` or `getElementById("message")`.
27
+
For example, to retrieve all div elements you can use this selector `$("div")`, or to retrieve all elements with a specific id use `$("#container")` instead of vanilla JavaScript: `getElementByTag("div")`, or `getElementById("container")`.
28
28
29
29
```javascript
30
-
$("p") // all paragraph elemenets
30
+
$("div") // all div elemenets
31
31
$("#container") // an element with the ID container
32
32
$(".total") // elements with the class total
33
33
$("ol#items") // ordered list elements with the ID items
@@ -108,24 +108,26 @@ We've learned in the previous lesson how to bind events on **click** by setting
108
108
<ahref="#"class="done"onclick="alert('Click event')">Show an alert box</a>
109
109
```
110
110
111
-
With jQuery we can achieve the same thing with an event listener. To listen for
112
-
events anywhere on the page we attach the event listener to the document
113
-
element.
111
+
With jQuery we can achieve the same thing with an event listener.
114
112
115
113
```js
116
114
$(document).on('click','.done', function() {
117
-
alert("Click event");
115
+
alert("Click event");
118
116
});
119
117
```
120
118
121
-
There are two differences between these examples:
119
+
Although these two examples do the same thing there are some differences.
122
120
123
-
- This event listener will listen for `click` on all the elements with class
124
-
`done`, i.e. one listener can listen to many elements.
125
-
- jQuery's `on()` method is dynamic, so if we add new items to the page with
126
-
class `done` the listener will listen to them as well.
121
+
##Event listeners
127
122
128
-
**Handling events**
123
+
- Event listeners are usually attached to the document element so they listen
124
+
for events anywhere on the page.
125
+
- The event listener uses a selector like `.done` to listen to specific
126
+
elements. A selector can match more than one element, so one listener can
127
+
listen to many elements.
128
+
- jQuery's `on()` method is dynamic, so if we add elements to the page that
129
+
match the selector, i.e. add an element with class `done`, then the listener
130
+
will automatically listen to them too.
129
131
130
132
To create your own event listener choose an `event` to listen for on the
131
133
elements matching a `selector`. Then put the code you want to run each time the
@@ -181,23 +183,27 @@ function addToList(item) {
181
183
182
184
}
183
185
```
184
-
In the `index.html` file, we already have an `<ol>` component, so we want to add each new item, as a list item. We can do that by constructing the html we need `"<li>" + item + "</li>"` and adding it to the list using `append()`.
186
+
In the `index.html` file there is an empty list, `<ol id="items">`, and we're going to add list items to it.
185
187
186
-
We can get the ordered list using the element tag and the id`$("ol#items")`
188
+
Target the empty list using its html tag and id:`$("ol#items")`.
187
189
188
-
Try running the function you've just written from the inspector
190
+
Then construct the html for a new list item `"<li>" + item + "</li>"` and add it to the end of the list using jQuery's `append()` method.
191
+
192
+
Try running the function you've just written from the browser console.
189
193
190
194
```js
191
195
addToList("build a website");
192
196
```
193
197
194
-
Now that you can successfully add items to the list, bind the function to the button with the id `#add-to-list`. To get the text from the input field, you can use `$("#item").val()`.
198
+
Now that you can successfully add items to the list, write an event handler to listen for the `click` event on the button. Tip: the button's id is `#add-to-list`.
199
+
200
+
Then use jQuery's `val()` method to get the text from the input field and run your `addToList()` function with the input field text as the argument from the event handler. Tip: the input field's id is `#item`.
195
201
196
202
###Bonus
197
-
After adding the item to the list
203
+
After adding the item to the list:
198
204
199
-
1. Empty the text field
200
-
2. Use `focus()` to place the cursor back to the text field
205
+
1. Empty the text field after adding the item to the list.
206
+
2. Use jQuery's `focus()`method to place the cursor back in the text field after clicking the button.
0 commit comments