Skip to content

Commit cfbf6d0

Browse files
committed
Merge pull request #142 from jamesjoshuahill/js-lesson-3-more-amends
JavaScript lesson 3, more detail
2 parents d9e5842 + 5c8bac6 commit cfbf6d0

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

js/lesson3/tutorial.md

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ jQuery is a JavaScript library that supplies you with functionality independent
2424

2525
Selectors are simplified in jQuery. You can access elements by element type, id or class, just like in `CSS`.
2626

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")`.
2828

2929
```javascript
30-
$("p") // all paragraph elemenets
30+
$("div") // all div elemenets
3131
$("#container") // an element with the ID container
3232
$(".total") // elements with the class total
3333
$("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
108108
<a href="#" class="done" onclick="alert('Click event')">Show an alert box</a>
109109
```
110110

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.
114112

115113
```js
116114
$(document).on('click','.done', function() {
117-
alert("Click event");
115+
alert("Click event");
118116
});
119117
```
120118

121-
There are two differences between these examples:
119+
Although these two examples do the same thing there are some differences.
122120

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
127122

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.
129131

130132
To create your own event listener choose an `event` to listen for on the
131133
elements matching a `selector`. Then put the code you want to run each time the
@@ -181,23 +183,27 @@ function addToList(item) {
181183

182184
}
183185
```
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.
185187

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")`.
187189

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.
189193

190194
```js
191195
addToList("build a website");
192196
```
193197

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`.
195201

196202
###Bonus
197-
After adding the item to the list
203+
After adding the item to the list:
198204

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.
201207

202208
###Label items
203209

0 commit comments

Comments
 (0)