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
+38-22Lines changed: 38 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ title: Introduction to jQuery
4
4
---
5
5
6
6
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.
8
8
9
9
In this session we will introduce jQuery, a very commonly used JavaScript library, that simplifies working with JavaScript.
10
10
@@ -18,13 +18,13 @@ Download the files required to begin working through the first tutorial example
18
18
19
19
#What is jQuery?
20
20
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.
22
22
23
23
##Selectors
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 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")`.
28
28
29
29
```javascript
30
30
$("p") // all paragraph elemenets
@@ -34,7 +34,7 @@ $("ol#items") // ordered list elements with the ID items
34
34
$("ol#items li") // list elements, within an ordered list with the id colors
35
35
```
36
36
37
-
You can also use CSS3 selectors
37
+
You can also use CSS3 selectors.
38
38
39
39
```javascript
40
40
$("input[type=text]"); // inputs of type text
@@ -43,33 +43,41 @@ $("li:odd"); // all odd numbered list items
43
43
$("li:first-child"); // the first child in a list
44
44
```
45
45
46
-
##Accessing attributes `attr()`
46
+
##Get and set HTML attributes `attr()`
47
47
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.
49
53
50
54
```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
53
57
```
54
58
55
-
##Changing CSS attributes
59
+
##Get and set CSS styles `css()`
56
60
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.
58
63
59
64
```js
60
65
var heading =$('h1');
66
+
heading.css('color');
61
67
heading.css('color', 'red');
62
68
```
63
69
64
-
##val()
70
+
##Get and set input values `val()`
65
71
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.
67
75
68
76
To empty an input field, you can set value to an empty string.
69
77
70
78
```javascript
79
+
$('input').val();
71
80
$('input').val("");
72
-
73
81
```
74
82
75
83
##Adding content
@@ -92,41 +100,49 @@ $('#container').replaceWith("<div>I love jQuery!</div>")
92
100
93
101
##Handling Events
94
102
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/).
96
104
97
105
We've learned in the previous lesson how to bind events on **click** by setting **onclick** to the HTML element..
98
106
99
107
```html
100
108
<ahref="#"class="done"onclick="alert('Click event')">Show an alert box</a>
101
109
```
102
110
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.
104
114
105
115
```js
106
116
$(document).on('click','.done', function() {
107
117
alert("Click event");
108
118
});
109
119
```
110
120
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
+
111
128
**Handling events**
112
129
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
+
113
134
```javascript
114
135
$(document).on(event, selector, function() {
115
136
// code to be executed when event occurs
116
137
});
117
138
```
118
139
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.
0 commit comments