Skip to content

Commit f268a3e

Browse files
committed
Merge pull request #138 from jamesjoshuahill/js-lesson-3-amends
Flesh out 'Introduction to jQuery'
2 parents 74b5fd4 + 386e748 commit f268a3e

File tree

1 file changed

+38
-22
lines changed

1 file changed

+38
-22
lines changed

js/lesson3/tutorial.md

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Introduction to jQuery
44
---
55

66

7-
So far, we've learned the basic of JavaScript. From variables, to understanding Objects, functions and how to manipulate the ​**D**​ocument ​**O**​bject ​**M**​odel.
7+
So far, we've learned the basics of JavaScript. From variables, to understanding Objects, functions and how to manipulate the ​**D**​ocument ​**O**​bject ​**M**​odel.
88

99
In this session we will introduce jQuery, a very commonly used JavaScript library, that simplifies working with JavaScript.
1010

@@ -18,13 +18,13 @@ Download the files required to begin working through the first tutorial example
1818

1919
#What is jQuery?
2020

21-
jQuery is a JavaScript library that supplies you with functionality independent of browser platform. It's very commonly used on the internet and enables you to do more with writing less code.
21+
jQuery is a JavaScript library that supplies you with functionality independent of browser platform. It's very commonly used on the internet and enables you to do more with less code.
2222

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

2929
```javascript
3030
$("p") // all paragraph elemenets
@@ -34,7 +34,7 @@ $("ol#items") // ordered list elements with the ID items
3434
$("ol#items li") // list elements, within an ordered list with the id colors
3535
```
3636

37-
You can also use CSS3 selectors
37+
You can also use CSS3 selectors.
3838

3939
```javascript
4040
$("input[type=text]"); // inputs of type text
@@ -43,33 +43,41 @@ $("li:odd"); // all odd numbered list items
4343
$("li:first-child"); // the first child in a list
4444
```
4545

46-
##Accessing attributes `attr()`
46+
##Get and set HTML attributes `attr()`
4747

48-
Using the `attr()` method you can retrieve any element attribute.
48+
Using `attr(attributeName)` you can retrieve the value of an attribute.
49+
50+
You can use the same method to set the value of an attribute:
51+
`attr(attributeName, value)`. Many jQuery methods can be used to both get and
52+
set.
4953

5054
```js
51-
$('#logo').attr('width')
52-
$('#logo').attr('width', 300)
55+
$('#logo').attr('width') // get width
56+
$('#logo').attr('width', 300) // set width to 300
5357
```
5458

55-
##Changing CSS attributes
59+
##Get and set CSS styles `css()`
5660

57-
You can get and set the CSS properties of an item by using the `css()` action.
61+
Like `attr()`, you can get and set CSS style properties with the `css()`
62+
method.
5863

5964
```js
6065
var heading = $('h1');
66+
heading.css('color');
6167
heading.css('color', 'red');
6268
```
6369

64-
##val()
70+
##Get and set input values `val()`
6571

66-
To set and get the text in an input field, you can use `val()`. Similar to `attr()` and `css()` you can use the function without any parameters to retrieve the value, and `val(value)` to update the value.
72+
Similar to `attr()` and `css()` you can use the `val()` function without any
73+
parameters to get the value of an input field, and `val(value)` to set the
74+
value.
6775

6876
To empty an input field, you can set value to an empty string.
6977

7078
```javascript
79+
$('input').val();
7180
$('input').val("");
72-
7381
```
7482

7583
##Adding content
@@ -92,41 +100,49 @@ $('#container').replaceWith("<div>I love jQuery!</div>")
92100
93101
##Handling Events
94102

95-
Events is what happens when you interact with a website. Some events that you can capture are a **change** in an input field, a mouse **click** or even **focus** on an element. You can find a [all the events here](http://api.jquery.com/category/events/)
103+
Events are what happen when you interact with a website. Some events that you can capture are a **change** in an input field, a mouse **click** or even **focus** on an element. You can find a list of [all the events here](http://api.jquery.com/category/events/).
96104

97105
We've learned in the previous lesson how to bind events on **click** by setting **onclick** to the HTML element..
98106

99107
```html
100108
<a href="#" class="done" onclick="alert('Click event')">Show an alert box</a>
101109
```
102110

103-
With jQuery we can achieve this by applying event listeners to the document of the page, listening for the event.
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.
104114

105115
```js
106116
$(document).on('click','.done', function() {
107117
alert("Click event");
108118
});
109119
```
110120

121+
There are two differences between these examples:
122+
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.
127+
111128
**Handling events**
112129

130+
To create your own event listener choose an `event` to listen for on the
131+
elements matching a `selector`. Then put the code you want to run each time the
132+
event occurs in the `function`.
133+
113134
```javascript
114135
$(document).on(event, selector, function() {
115136
// code to be executed when event occurs
116137
});
117138
```
118139

119-
Using the `on()` method, means that the click event will work even if we add new items to the DOM dynamically.
120-
121-
> `$(this)` is the element that we have triggered the event from.
122-
123140
##Waiting for the page to load
124141

125142
```js
126143
$(document).ready(function() {
127-
// here go all the interactions
128-
129-
/* click, mouseover, change etc */
144+
// here go all the listeners
145+
// e.g. on click, mouseover, change etc
130146
});
131147
```
132148

0 commit comments

Comments
 (0)