Lesson 204 03 oct13-1500-ay

25
Unit 2: jQuery Lesson 4: Events October 2, 2013

Transcript of Lesson 204 03 oct13-1500-ay

Page 1: Lesson 204 03 oct13-1500-ay

Unit 2: jQueryLesson 4: Events

October 2, 2013

Page 2: Lesson 204 03 oct13-1500-ay

2

Lesson 4: Events

Introduction to jQuery

Syntax and Structure Abstraction Events

HTML and Forms

Search Engine

Optimization

Learning to Use CSS

Introduction to CSS

Reusing Code

3 Ways to Use CSS

Separation of Concerns

Launching Your Own Website

Lesson 1 Lesson 2 Lesson 3 Lesson 4

Lesson 8 Lesson 7 Lesson 6 Lesson 5

Lesson 9 Lesson 10 Lesson 11 Lesson 12

Page 3: Lesson 204 03 oct13-1500-ay

3

Recap from last time (I)

• Abstraction is the process of hiding the complex parts of a system so that only the important details can be seen

• A gas pedal is an example of an abstraction – it lets us control the speed of the car without needing to understand what happens under the hood

Page 4: Lesson 204 03 oct13-1500-ay

Recap from last time (II)

4

• Similarly, jQuery is an abstraction of Javascript – it lets us use Javascript without having to understand the implementation details

$(document).ready(function() {

$(‘#clickedElement’).click(function() {

$(‘#fadedElement’).fadeOut(); });});

jQuery code

Page 5: Lesson 204 03 oct13-1500-ay

Recap from last time (III)

5

• Similarly, jQuery is an abstraction of Javascript – it lets us use Javascript without having to understand the implementation details

$(document).ready(function() {

$(‘#clickedElement’).click(function() {

$(‘#fadedElement’).fadeOut(); });});

jQuery code English translation

Select the document. When it is readydo the following:

Select the element with id named clickedElement. If clicked, do the following:

Select the element with id named fadedElement and make it fade out

Page 6: Lesson 204 03 oct13-1500-ay

6

Events are an important part of jQuery

• We saw in Lesson 2 that jQuery often has the same structure

• Today we’ll be focusing on understanding the part of the structure that relates to events

$(document).ready(function() {

$(pageElement).someEvent(function() {

$(thingToChange).someEffect();

});});

jQuery code English translation

When the document is ready, do the following:

When someEvent happens to pageElement, do the following:

Make someEffect happen to thingToChange

Page 7: Lesson 204 03 oct13-1500-ay

7

What is a jQuery event?

• An event is any action that a user takes on a web page, such as:• Double-clicking on a button • Single-clicking on a button• Hovering the mouse over an image

• Events are important because they allow us to interact with our users by responding to their actions

Tom Cruise interacts with a fancy computer in the 2002

movie Minority Report

Page 8: Lesson 204 03 oct13-1500-ay

8

Events are often used to trigger an effect

• A good example of an event in real-life is stepping on the gas pedal of a car

• In this case, the driver (the user) initiates the event by pressing down on the gas pedal

• This event triggers the car to increase its speed

The event (the cause) The resulting effect

Page 9: Lesson 204 03 oct13-1500-ay

9

jQuery events work in the same way

• jQuery events are similar, except that we get to decide which events to respond to

Event Effect

If user

If user

If user

double-clicks on a button,

single-clicks on a button

hovers over the image

turn the text background color redthen

then

then

turn the text background color red

turn the text background color red

Page 10: Lesson 204 03 oct13-1500-ay

10

Time for an example (I)

$(document).ready(function() {

$(‘#button’).dblclick(function() {

$(p).css(“background-color”: “red”);

});});

jQuery code English translation

Page 11: Lesson 204 03 oct13-1500-ay

11

Time for an example (II)

$(document).ready(function() {

$(‘#button’).dblclick(function() {

$(p).css(“background-color”: “red”);

});});

jQuery code English translation

When the document is ready, do the following:

Page 12: Lesson 204 03 oct13-1500-ay

12

Time for an example (III)

$(document).ready(function() {

$(‘#button’).dblclick(function() {

$(p).css(“background-color”: “red”);

});});

jQuery code English translation

When the document is ready, do the following:

When the HTML element with id ‘button’ is double-clicked, do the following:

Page 13: Lesson 204 03 oct13-1500-ay

13

Time for an example (IV)

$(document).ready(function() {

$(‘#button’).dblclick(function() {

$(p).css(“background-color”: “red”);

});});

jQuery code English translation

When the document is ready, do the following:

When the HTML element with id ‘button’ is double-clicked, do the following

Select the HTML element with <p> tag and edit its CSS styling for background color to red

Page 14: Lesson 204 03 oct13-1500-ay

14

Time for an example (V)

$(document).ready(function() {

$(‘#button’).dblclick(function() {

$(p).css(“background-color”: “red”);

});});

jQuery code English translation

When the document is ready, do the following:

When the HTML element with id ‘button’ is double-clicked, do the following

Select the HTML element with <p> tag and edit its CSS styling for background color to red

Syntax notes

$(element) means “select the element”

Page 15: Lesson 204 03 oct13-1500-ay

15

Time for an example (VI)

$(document).ready(function() {

$(‘#button’).dblclick(function() {

$(p).css(“background-color”: “red”);

});});

jQuery code English translation

When the document is ready, do the following:

When the HTML element with id ‘button’ is double-clicked, do the following

Select the HTML element with <p> tag and edit its CSS styling for background color to red

Syntax notes

$(element) means “select the element”

$(element).action() means “do this action to the element”

Page 16: Lesson 204 03 oct13-1500-ay

16

Time for an example (VII)

$(document).ready(function() {

$(‘#button’).dblclick(function() {

$(p).css(“background-color”: “red”);

});});

jQuery code English translation

When the document is ready, do the following:

When the HTML element with id ‘button’ is double-clicked, do the following

Select the HTML element with <p> tag and edit its CSS styling for background color to red

Syntax notes

$(element) means “select the element”

$(element).action() means “do this action to the element”

function() means “do the following”

Page 17: Lesson 204 03 oct13-1500-ay

17

Time for an example (VIII)

$(document).ready(function() {

$(‘#button’).dblclick(function() {

$(p).css(“background-color”: “red”);

});});

jQuery code Before

After

Need image here (text, button, and image on page)

?Can you figure out how the page to the right will change?

Page 18: Lesson 204 03 oct13-1500-ay

18

Time for an example (IX)

$(document).ready(function() {

$(‘#button’).dblclick(function() {

$(p).css(“background-color”: “red”);

});});

jQuery code Before

After

Need image here (text now has red background)

Need image here (text, button, and image on page)

After double-clicking the button, the text now has red background

Page 19: Lesson 204 03 oct13-1500-ay

19

Time for an example (X)

$(document).ready(function() {

$(‘#button’).dblclick(function() {

$(p).css(“background-color”: “red”);

});});

jQuery code Before

After

Need image here (text now has red background)

Need image here (text, button, and image on page)

After double-clicking the button, the text now has red background

Page 20: Lesson 204 03 oct13-1500-ay

20

Events in action! (I)

• jQuery makes it easy for us to use different events

• If we change our minds and want the text background color to become red when the user single-clicks on the button, all we need to do is swap out our one line of jQuery event code

$(document).ready(function() {

$(‘#button’).dblclick(function() {

$(p).css(“background-color”: “red”);

});});

$(document).ready(function() {

$(‘#button).click(function() {

$(p).css(“background-color”: “red”);

});});

Page 21: Lesson 204 03 oct13-1500-ay

21

Events in action! (II)

• If we change our minds again and want the text background color to become red when the user hovers over the image, all we need to do is swap out our line of jQuery event code

$(document).ready(function() {

$(‘#button’).dblclick(function() {

$(p).css(“background-color”: “red”);

});});

$(document).ready(function() {

$(‘#img’).hover(function() {

$(p).css(“background-color”: “red”);

});});

Page 22: Lesson 204 03 oct13-1500-ay

22

Summary (I)

• An event is any action that a user takes on a web page, such as:• Double-clicking on a button • Single-clicking on a button• Hovering the mouse over an image

Event Effect

If user

If user

If user

double-clicks on a button,

single-clicks on a button

hovers over the image

turn the text background color redthen

then

then

turn the text background color red

turn the text background color red

Page 23: Lesson 204 03 oct13-1500-ay

23

Summary (II)

• jQuery makes it easy for us to use different events

• If we change our minds and want to trigger the effect based on a different event, all we need to do is swap out our line of jQuery event code

$(document).ready(function() {

$(‘#button’).dblclick(function() {

$(p).css(“background-color”: “red”);

});});

Page 24: Lesson 204 03 oct13-1500-ay

24

Summary (III)

• jQuery makes it easy for us to use different events

• If we change our minds and want to trigger the effect based on a different event, all we need to do is swap out our line of jQuery event code

$(document).ready(function() {

$(‘#button’).dblclick(function() {

$(p).css(“background-color”: “red”);

});});

$(document).ready(function() {

$(‘#button).click(function() {

$(p).css(“background-color”: “red”);

});});

Page 25: Lesson 204 03 oct13-1500-ay

25

What to do on your own

1. Go to URL to complete the Codecademy course online

2. Do the practice set on the material learned

3. Take the follow-up quiz to test your understanding