SlideShare una empresa de Scribd logo
1 de 70
HTML5, CSS3,
and JavaScript
6th Edition
Working with Events and Styles
Tutorial 11
Carey
XPXPXPXPXPObjectives
• Create an event handler
• Reference an attribute from a page element
• Change the inline style of a page element
• Create code for mouse events
• Create code for keyboard events
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 2
XPXPXPXPXPObjectives (continued)
• Design and apply custom cursors
• Create and apply anonymous functions
• Work with alert, confirm, and prompt dialog
boxes
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 3
XPXPXPXPXPIntroducing JavaScript Events
• JavaScript programs run in response to events
• Events: Actions initiated by the user or by the
browser
• Example:
– Clicking an object on a form
– Closing a web page
• JavaScript events can be used to build a Hanjie
puzzle, i.e., a grid in which each grid cell is
either filled or left empty
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 4
XPXPXPXPXPIntroducing JavaScript Events (continued)
• The Hanjie puzzle app contains three puzzles
named puzzle1, puzzle2, and puzzle3
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 5
XPXPXPXPXPCreating an Event Handler
• Event handler: A property that controls how
an object will respond to an event
• Event handler waits until the event occurs and
then responds by running a function or
command block to execute an action
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 6
XPXPXPXPXPCreating an Event Handler (continued 1)
• Event handlers can be added to a page
element using the following attribute:
<element onevent = “script”>
where element is the element in which the
event occurs, event is the name of the event,
and script are the commands that the
browser runs in response to the event
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 7
XPXPXPXPXPCreating an Event Handler (continued 2)
• Event handlers can also be defined as object
properties using the command
object.onevent = function;
where object is the object in which the event
occurs, event is the name of the event, and
function is the name of a function run in
response to the event
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 8
XPXPXPXPXPCreating an Event Handler (continued 3)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 9
XPXPXPXPXPCreating an Event Handler (continued 4)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 10
XPXPXPXPXPCreating an Event Handler (continued 5)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 11
XPXPXPXPXPCreating an Event Handler (continued 6)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 12
• Add an event handler to the buttons in the
Figure 11-8
• Apply the following onclick event handler to
respond to a mouse click:
object.onclick = function;
where object is the page element that is
being clicked and function is the function run
in response to the click event
XPXPXPXPXPCreating an Event Handler (continued 7)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 13
XPXPXPXPXPCreating an Event Handler (continued 8)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 14
• The challenge lies in determining which button
was clicked
• There is no way of knowing which puzzle to
load into the page without knowing which
button activated the onclick event handler
• This information can be determined using the
event object
XPXPXPXPXPUsing the Event Object
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 15
• Event object is an object that contains
properties and methods associated with an
event
• Example: The action of clicking a mouse
button generates an event object containing
information such as which mouse button was
clicked, where in the page it was clicked, the
time at which it was clicked, and so forth
XPXPXPXPXPUsing the Event Object (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 16
• Event object is passed as an argument to the
function handling the event to retrieve the
information contained in the event object
• Example:
function myFunction(evt) {
function code
}
where evt is the name assigned to the
parameter receiving the event object from the
event handler
XPXPXPXPXPUsing the Event Object (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 17
XPXPXPXPXPUsing the Event Object (continued 3)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 18
XPXPXPXPXPExploring Object Properties
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 19
• JavaScript object properties that mirror HTML
attributes follow certain conventions
• All JavaScript properties must begin with a
lowercase letter
• If the HTML attribute consists of multiple
words, JavaScript property follows a format
known as camel case, i.e., the initial word is in
lowercase and the first letter of each
subsequent word is in uppercase
XPXPXPXPXPExploring Object Properties (continued)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 20
• If the name of the HTML attribute is a reserved
JavaScript name or keyword, the
corresponding JavaScript property is often
prefaced with the text string html
• class attribute is an exception to this
convention because the class name is reserved
by JavaScript for other purposes
• References to the HTML class attribute use the
className property
XPXPXPXPXPObject Properties and Inline Styles
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 21
• Inline styles for each page element can be
applied using the following style attribute:
<element style = “property:value”> …
where
– element is the page element
– property is a CSS style property
– value is the value assigned to that property
XPXPXPXPXP
Object Properties and Inline Styles
(continued)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 22
• The equivalent command in JavaScript is
object.style.property = “value”;
with the property style written in camel case
• Inline styles have precedence over style sheets
XPXPXPXPXP
Creating Object Collections with CSS
Selectors
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 23
• Change the background color of all table cells
by applying the following CSS style rule for all
td elements:
table#hanjieGrid td {
background-color: rgb(233, 207, 29);
}
XPXPXPXPXP
Creating Object Collections with CSS
Selectors (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 24
• In JavaScript, to change the background color
of all table cells, you must first define an
object collection based on a CSS selector using
the following querySelectorAll() method:
document.querySelectorAll(selector)
where selector is the CSS selector that the
object collection is based on
XPXPXPXPXP
Creating Object Collections with CSS
Selectors (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 25
• Once the object collection has been defined,
change the background-color style of each td
element by applying the backgroundColor
property to the objects in the object collection
• Reference only the first element that matches
a selector pattern using the following
JavaScript method:
document.querySelector(selector)
where selector is a CSS selector
XPXPXPXPXP
Creating Object Collections with CSS
Selectors (continued 3)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 26
XPXPXPXPXP
Creating Object Collections with CSS
Selectors (continued 4)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 27
XPXPXPXPXPWorking with Mouse Events
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 28
• JavaScript supports events associated with the
mouse such as clicking, right-clicking, double-
clicking, and moving the pointer over and out
of page elements
XPXPXPXPXPWorking with Mouse Events (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 29
• A mouse action can be comprised of several
events
• The action of clicking the mouse button is
comprised of three events, fired in the
following order:
– mousedown (the button is pressed down)
– mouseup (the button is released)
– click (the button has been pressed and released)
XPXPXPXPXPWorking with Mouse Events (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 30
• The event object for mouse events has a set of
properties that can be used to give specific
information about the state of the mouse
XPXPXPXPXPWorking with Mouse Events (continued 3)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 31
XPXPXPXPXPIntroducing the Event Model
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 32
• Event model: Describes how events and
objects interact within the web page and web
browser
• The process in which a single event is applied
to a hierarchy of objects is part of the event
model
XPXPXPXPXPIntroducing the Event Model (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 33
• Once an event has been initiated, it
propagates through the object hierarchy in
three phases
– Capture phase: The event moves down the object
hierarchy starting from the root element (the
browser window) and moving inward until it
reaches the object that initiated the event
– Target phase: The event has reached the target of
the event object and no longer moves down the
object hierarchy
XPXPXPXPXPIntroducing the Event Model (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 34
– Bubbling phase: The event propagates up the
object hierarchy back to the root element
(browser window) where the propagation stops
XPXPXPXPXPIntroducing the Event Model (continued 3)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 35
• Limitations of event handlers
– Event handlers, such as onclick and
onmousedown, respond to events during the
target phase, but they do not recognize the
propagation of events through the capture and
bubbling phases
– Only one function can be applied to an event
handler at a time
XPXPXPXPXPAdding an Event Listener
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 36
• Event listener: Listens for events as they
propagate through the capture, target, and
bubble phases, allowing the script to respond
to an event within any phase
• Unlike event handlers, more than one function
can be applied to an event using event
listeners
XPXPXPXPXPAdding an Event Listener (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 37
• Add an event listener to an object by applying
the addEventListener()method
object.addEventListener(event,
function [, capture = false]);
where
– object is the object in which the event occurs
– event is the event
– function is the function that is run in response
to the event
XPXPXPXPXPAdding an Event Listener (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 38
– capture is an optional Boolean value
• true indicates that the function is executed during the
capture phase
• false (the default) indicates that the function is run
during the bubbling phase
XPXPXPXPXPRemoving an Event Listener
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 39
• The event model allows to remove event
listeners from the document by applying
removeEventListener() method
object.removeEventListener(event,
function [, capture = false]);
where object, event, function, and capture
have the same meanings as the
addEventListener() method
XPXPXPXPXPRemoving an Event Listener (continued)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 40
XPXPXPXPXPControlling Event Propagation
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 41
• The browser has its own default responses to
events
• Apply the following preventDefault()
method to the event object to prevent the
occurrence of the browser’s default actions:
evt.preventDefault()
XPXPXPXPXP
Controlling Event Propagation
(continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 42
• Alternatively, you can prevent the browser’s
default action by returning the value false
from the event handler function
• The return false; statement does not
prevent default actions if event listeners are
used in place of event handlers
XPXPXPXPXP
Controlling Event Propagation
(continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 43
XPXPXPXPXPExploring Keyboard Events
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 44
• JavaScript supports the keydown, keypress,
and keyup events that allow users to interact
with the web page and browser through the
keyboard
XPXPXPXPXPExploring Keyboard Events (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 45
• The keydown and keypress events are similar
in name; the difference between them is as
follows:
– The keydown and keyup events are fired in
response to the physical act of pressing the key
down and of a key moving up when it is no longer
held down
– The keypress event is fired in response to the
computer generating a character
XPXPXPXPXPExploring Keyboard Events (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 46
XPXPXPXPXPExploring Keyboard Events (continued 3)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 47
• The value associated with a key event property
is affected by the event itself
• For the keypress event, the charCode,
keyCode, and which properties all return a
Unicode character number
• Sample values of the charCode, keyCode,
which, and key properties for different
keyboard keys and events are shown in Figure
11-29
XPXPXPXPXPExploring Keyboard Events (continued 4)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 48
XPXPXPXPXPExploring Keyboard Events (continued 5)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 49
• Modifier keys: Alt, Ctrl, Shift, and Command
keys
• In addition to character keys, JavaScript
supports the modifier keys through the use of
the altKey, ctrlKey, shiftKey, and metaKey
properties
XPXPXPXPXPExploring Keyboard Events (continued 6)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 50
XPXPXPXPXPChanging the Cursor Style
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 51
• Cursors can be defined using the following CSS
cursor style:
cursor: cursorTypes;
where cursorTypes is a comma-separated list
of cursor types
XPXPXPXPXPChanging the Cursor Style (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 52
• JavaScript command to define cursors is as
follows:
object.style.cursor = cursorTypes;
where object is the page object that will
display the cursor style when hovered over by
the mouse pointer
XPXPXPXPXPChanging the Cursor Style (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 53
• Create a customized cursor from an image file
using url(image)where image is an image file
• Example: cursor: url(jpf_pencil.png),
pointer;
XPXPXPXPXPChanging the Cursor Style (continued 3)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 54
• By default, the click point for a cursor is
located in the top-left corner of the cursor
image at the coordinates (0, 0)
• Specify a different location by adding the (x, y)
coordinates of the click point to the cursor
definition as follows:
url(image) x y
where x is the x-coordinate and y is the y-
coordinate of the click point in pixels
XPXPXPXPXPWorking with Functions as Objects
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 55
• Everything in JavaScript is an object, including
functions
• Anything that can be done with an object can
be done with a function, including
– Storing a function as variable
– Storing a function as an object property
XPXPXPXPXP
Working with Functions as Objects
(continued)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 56
– Using one function as a parameter in another
function
– Nesting one function within another function
– Returning a function as the result of another
function
– Modifying the properties of a function
XPXPXPXPXP
Function Declarations and Function
Operators
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 57
• The following hello() function is created using
the function declaration format:
function hello(){
alert(“Welcome to Hanjie!”);}
• Function operator: The definition of the
function becomes the variable’s “value”
var hello = function (){
alert(“Welcome to Hanjie!”);
}
XPXPXPXPXP
Function Declarations and Function
Operators (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 58
• The two ways of defining the hello() function
differ in how they are stored
– Functions defined with a function declaration are
created and saved as the browser parses the code
prior to the script being run
• Since the function is already stored in memory, the
statements that run the function can be placed prior to
the statement that declares the function
XPXPXPXPXP
Function Declarations and Function
Operators (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 59
– Function operators are evaluated as they appear in
the script after the code has been parsed by the
browser
• Function operators are more flexible than function
declarations, allowing a function to be placed anywhere
a variable can be placed
XPXPXPXPXPAnonymous Functions
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 60
• Anonymous function has function declaration
without the function name
• Example: The following structure is an
anonymous function:
function() {
commands
}
• An anonymous function can be inserted into
any expression where a function reference is
required
XPXPXPXPXPAnonymous Functions (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 61
• Functions that are named are called named
functions
• Anonymous functions are more concise and
easier to manage because the function is
directly included with the expression that
invokes it
• Anonymous functions limit the scope of the
function to exactly where it is needed
XPXPXPXPXPAnonymous Functions (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 62
XPXPXPXPXP
Passing Variable Values into Anonymous
Functions
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 63
• JavaScript supports two types of variables:
– Global variables: Declared outside of any function
and thus are accessible throughout the app
– Local variables: Declared within a function and are
only accessible to code within that function
XPXPXPXPXP
Passing Variable Values into Anonymous
Functions (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 64
• Global variables should be avoided when
possible because
– global variables are accessible to every function in
the application
– the task of tracking which functions are using and
modifying the global variables becomes
increasingly difficult as the application grows in
size and complexity
XPXPXPXPXP
Passing Variable Values into Anonymous
Functions (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 65
• An advantage of using anonymous functions is
that they reduce the need for global variables
because they perform their actions locally
within a function
• One of the challenges of anonymous functions
is keeping track of all of the nested levels of
functions and procedures
XPXPXPXPXP
Passing Variable Values into Anonymous
Functions (continued 3)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 66
XPXPXPXPXPDisplaying Dialog Boxes
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 67
• Alert dialog box can be created using the
following alert() method:
alert(text)
where text is the message displayed in the
alert dialog box
• When an alert dialog box is displayed, the
execution of the program code halts until the
user clicks the OK button in the dialog box
XPXPXPXPXPDisplaying Dialog Boxes (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 68
• JavaScript supports confirmation and prompt
dialog boxes
XPXPXPXPXPDisplaying Dialog Boxes (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 69
XPXPXPXPXPDisplaying Dialog Boxes (continued 3)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 70

Más contenido relacionado

La actualidad más candente

Chapter 8 Enhancing a website with multimedia
Chapter 8 Enhancing a website with multimediaChapter 8 Enhancing a website with multimedia
Chapter 8 Enhancing a website with multimediaDr. Ahmed Al Zaidy
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScriptMarlon Jamera
 
Responsive web design ppt
Responsive web design pptResponsive web design ppt
Responsive web design pptNAWAZ KHAN
 
HTML & CSS: Chapter 03
HTML & CSS: Chapter 03HTML & CSS: Chapter 03
HTML & CSS: Chapter 03Steve Guinan
 
JavaScript - Chapter 5 - Operators
 JavaScript - Chapter 5 - Operators JavaScript - Chapter 5 - Operators
JavaScript - Chapter 5 - OperatorsWebStackAcademy
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript ProgrammingSehwan Noh
 
HTML & CSS: Chapter 06
HTML & CSS: Chapter 06HTML & CSS: Chapter 06
HTML & CSS: Chapter 06Steve Guinan
 
Adobe Photoshop
Adobe Photoshop Adobe Photoshop
Adobe Photoshop Mujeeb Riaz
 
HTML & CSS: Chapter 07
HTML & CSS: Chapter 07HTML & CSS: Chapter 07
HTML & CSS: Chapter 07Steve Guinan
 
Introduction to web programming with JavaScript
Introduction to web programming with JavaScriptIntroduction to web programming with JavaScript
Introduction to web programming with JavaScriptT11 Sessions
 
JavaScript Conditional Statements
JavaScript Conditional StatementsJavaScript Conditional Statements
JavaScript Conditional StatementsMarlon Jamera
 
Html phrase tags
Html phrase tagsHtml phrase tags
Html phrase tagseShikshak
 
InDesign: Colors
InDesign: ColorsInDesign: Colors
InDesign: ColorsSam Georgi
 

La actualidad más candente (20)

Chapter 8 Enhancing a website with multimedia
Chapter 8 Enhancing a website with multimediaChapter 8 Enhancing a website with multimedia
Chapter 8 Enhancing a website with multimedia
 
CSS
CSSCSS
CSS
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Responsive web design ppt
Responsive web design pptResponsive web design ppt
Responsive web design ppt
 
Pagemaker
PagemakerPagemaker
Pagemaker
 
CSS
CSSCSS
CSS
 
HTML & CSS: Chapter 03
HTML & CSS: Chapter 03HTML & CSS: Chapter 03
HTML & CSS: Chapter 03
 
JavaScript - Chapter 5 - Operators
 JavaScript - Chapter 5 - Operators JavaScript - Chapter 5 - Operators
JavaScript - Chapter 5 - Operators
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript Programming
 
HTML & CSS: Chapter 06
HTML & CSS: Chapter 06HTML & CSS: Chapter 06
HTML & CSS: Chapter 06
 
SASS - CSS with Superpower
SASS - CSS with SuperpowerSASS - CSS with Superpower
SASS - CSS with Superpower
 
Adobe Photoshop
Adobe Photoshop Adobe Photoshop
Adobe Photoshop
 
HTML & CSS: Chapter 07
HTML & CSS: Chapter 07HTML & CSS: Chapter 07
HTML & CSS: Chapter 07
 
First review presentation
First review presentationFirst review presentation
First review presentation
 
Introduction to web programming with JavaScript
Introduction to web programming with JavaScriptIntroduction to web programming with JavaScript
Introduction to web programming with JavaScript
 
Javascript
JavascriptJavascript
Javascript
 
Flex box
Flex boxFlex box
Flex box
 
JavaScript Conditional Statements
JavaScript Conditional StatementsJavaScript Conditional Statements
JavaScript Conditional Statements
 
Html phrase tags
Html phrase tagsHtml phrase tags
Html phrase tags
 
InDesign: Colors
InDesign: ColorsInDesign: Colors
InDesign: Colors
 

Similar a Chapter 11 Working with Events and Styles

Chapter 14 Exploring Object-based Programming
Chapter 14 Exploring Object-based ProgrammingChapter 14 Exploring Object-based Programming
Chapter 14 Exploring Object-based ProgrammingDr. Ahmed Al Zaidy
 
Chapter 13 Programming for web forms
Chapter 13 Programming for web formsChapter 13 Programming for web forms
Chapter 13 Programming for web formsDr. Ahmed Al Zaidy
 
Chapter 7 Designing a web form
Chapter 7 Designing a web formChapter 7 Designing a web form
Chapter 7 Designing a web formDr. Ahmed Al Zaidy
 
9781305503922t5-200824144338.pdf
9781305503922t5-200824144338.pdf9781305503922t5-200824144338.pdf
9781305503922t5-200824144338.pdfHaboonMohmed
 
Chapter 3 Designing a Page Layout
Chapter 3 Designing a Page LayoutChapter 3 Designing a Page Layout
Chapter 3 Designing a Page LayoutDr. Ahmed Al Zaidy
 
Chapter 12 Working with Document nodes and style sheets
Chapter 12 Working with Document nodes and style sheetsChapter 12 Working with Document nodes and style sheets
Chapter 12 Working with Document nodes and style sheetsDr. Ahmed Al Zaidy
 
CS101- Introduction to Computing- Lecture 32
CS101- Introduction to Computing- Lecture 32CS101- Introduction to Computing- Lecture 32
CS101- Introduction to Computing- Lecture 32Bilal Ahmed
 
Javascript programming using the document object model
Javascript programming using the document object modelJavascript programming using the document object model
Javascript programming using the document object modelNicole Ryan
 
javascript Event Handling and introduction to event.ppt
javascript Event Handling and introduction to event.pptjavascript Event Handling and introduction to event.ppt
javascript Event Handling and introduction to event.pptLalith86
 
WPF (Windows Presentation Foundation Unit 01)
WPF (Windows Presentation Foundation Unit 01)WPF (Windows Presentation Foundation Unit 01)
WPF (Windows Presentation Foundation Unit 01)Prashanth Shivakumar
 
INTRODUCTION TO CLIENT SIDE PROGRAMMING
INTRODUCTION TO CLIENT SIDE PROGRAMMINGINTRODUCTION TO CLIENT SIDE PROGRAMMING
INTRODUCTION TO CLIENT SIDE PROGRAMMINGProf Ansari
 
Class notes(week 3) on class objects and methods
Class notes(week 3) on class objects and methodsClass notes(week 3) on class objects and methods
Class notes(week 3) on class objects and methodsKuntal Bhowmick
 

Similar a Chapter 11 Working with Events and Styles (20)

Chapter 14 Exploring Object-based Programming
Chapter 14 Exploring Object-based ProgrammingChapter 14 Exploring Object-based Programming
Chapter 14 Exploring Object-based Programming
 
Chapter 13 Programming for web forms
Chapter 13 Programming for web formsChapter 13 Programming for web forms
Chapter 13 Programming for web forms
 
Chapter 7 Designing a web form
Chapter 7 Designing a web formChapter 7 Designing a web form
Chapter 7 Designing a web form
 
html5 tutorial.pdf
html5 tutorial.pdfhtml5 tutorial.pdf
html5 tutorial.pdf
 
9781305503922t5-200824144338.pdf
9781305503922t5-200824144338.pdf9781305503922t5-200824144338.pdf
9781305503922t5-200824144338.pdf
 
Ddpz2613 topic9 java
Ddpz2613 topic9 javaDdpz2613 topic9 java
Ddpz2613 topic9 java
 
Extjs
ExtjsExtjs
Extjs
 
Javascript Workshop
Javascript WorkshopJavascript Workshop
Javascript Workshop
 
Chapter 3 Designing a Page Layout
Chapter 3 Designing a Page LayoutChapter 3 Designing a Page Layout
Chapter 3 Designing a Page Layout
 
javaScript and jQuery
javaScript and jQueryjavaScript and jQuery
javaScript and jQuery
 
Event Programming JavaScript
Event Programming JavaScriptEvent Programming JavaScript
Event Programming JavaScript
 
Chapter 12 Working with Document nodes and style sheets
Chapter 12 Working with Document nodes and style sheetsChapter 12 Working with Document nodes and style sheets
Chapter 12 Working with Document nodes and style sheets
 
IP Unit 2.pptx
IP Unit 2.pptxIP Unit 2.pptx
IP Unit 2.pptx
 
CS101- Introduction to Computing- Lecture 32
CS101- Introduction to Computing- Lecture 32CS101- Introduction to Computing- Lecture 32
CS101- Introduction to Computing- Lecture 32
 
Javascript programming using the document object model
Javascript programming using the document object modelJavascript programming using the document object model
Javascript programming using the document object model
 
javascript Event Handling and introduction to event.ppt
javascript Event Handling and introduction to event.pptjavascript Event Handling and introduction to event.ppt
javascript Event Handling and introduction to event.ppt
 
WPF (Windows Presentation Foundation Unit 01)
WPF (Windows Presentation Foundation Unit 01)WPF (Windows Presentation Foundation Unit 01)
WPF (Windows Presentation Foundation Unit 01)
 
INTRODUCTION TO CLIENT SIDE PROGRAMMING
INTRODUCTION TO CLIENT SIDE PROGRAMMINGINTRODUCTION TO CLIENT SIDE PROGRAMMING
INTRODUCTION TO CLIENT SIDE PROGRAMMING
 
J query
J queryJ query
J query
 
Class notes(week 3) on class objects and methods
Class notes(week 3) on class objects and methodsClass notes(week 3) on class objects and methods
Class notes(week 3) on class objects and methods
 

Más de Dr. Ahmed Al Zaidy

Chapter 10 Exploring arrays, loops, and conditional statements
Chapter 10 Exploring arrays, loops, and conditional statementsChapter 10 Exploring arrays, loops, and conditional statements
Chapter 10 Exploring arrays, loops, and conditional statementsDr. Ahmed Al Zaidy
 
Chapter 6 Working with Tables and Columns
Chapter 6 Working with Tables and ColumnsChapter 6 Working with Tables and Columns
Chapter 6 Working with Tables and ColumnsDr. Ahmed Al Zaidy
 
Chapter 2 Getting Started with CSS
Chapter 2 Getting Started with CSSChapter 2 Getting Started with CSS
Chapter 2 Getting Started with CSSDr. Ahmed Al Zaidy
 
Chapter 1 Getting Started with HTML5
Chapter 1 Getting Started with HTML5Chapter 1 Getting Started with HTML5
Chapter 1 Getting Started with HTML5Dr. Ahmed Al Zaidy
 
testing throughout-the-software-life-cycle-section-2
testing throughout-the-software-life-cycle-section-2testing throughout-the-software-life-cycle-section-2
testing throughout-the-software-life-cycle-section-2Dr. Ahmed Al Zaidy
 
Chapter 14 Business Continuity
Chapter 14 Business ContinuityChapter 14 Business Continuity
Chapter 14 Business ContinuityDr. Ahmed Al Zaidy
 
Chapter 13 Vulnerability Assessment and Data Security
Chapter 13 Vulnerability Assessment and Data SecurityChapter 13 Vulnerability Assessment and Data Security
Chapter 13 Vulnerability Assessment and Data SecurityDr. Ahmed Al Zaidy
 
Chapter 11 Authentication and Account Management
Chapter 11 Authentication and Account ManagementChapter 11 Authentication and Account Management
Chapter 11 Authentication and Account ManagementDr. Ahmed Al Zaidy
 
Chapter 10 Mobile and Embedded Device Security
Chapter 10 Mobile and Embedded Device Security Chapter 10 Mobile and Embedded Device Security
Chapter 10 Mobile and Embedded Device Security Dr. Ahmed Al Zaidy
 
Chapter 9 Client and application Security
Chapter 9 Client and application SecurityChapter 9 Client and application Security
Chapter 9 Client and application SecurityDr. Ahmed Al Zaidy
 
Chapter 8 Wireless Network Security
Chapter 8 Wireless Network SecurityChapter 8 Wireless Network Security
Chapter 8 Wireless Network SecurityDr. Ahmed Al Zaidy
 
Chapter 7 Administering a Secure Network
Chapter 7 Administering a Secure Network Chapter 7 Administering a Secure Network
Chapter 7 Administering a Secure Network Dr. Ahmed Al Zaidy
 
Chapter 6Network Security Devices, Design, and Technology
Chapter 6Network Security Devices, Design, and TechnologyChapter 6Network Security Devices, Design, and Technology
Chapter 6Network Security Devices, Design, and TechnologyDr. Ahmed Al Zaidy
 
Chapter 5 Networking and Server Attacks
Chapter 5 Networking and Server AttacksChapter 5 Networking and Server Attacks
Chapter 5 Networking and Server AttacksDr. Ahmed Al Zaidy
 
Chapter 4 Advanced Cryptography and P K I
Chapter 4 Advanced Cryptography and P K IChapter 4 Advanced Cryptography and P K I
Chapter 4 Advanced Cryptography and P K IDr. Ahmed Al Zaidy
 

Más de Dr. Ahmed Al Zaidy (20)

Chapter 10 Exploring arrays, loops, and conditional statements
Chapter 10 Exploring arrays, loops, and conditional statementsChapter 10 Exploring arrays, loops, and conditional statements
Chapter 10 Exploring arrays, loops, and conditional statements
 
Chapter 6 Working with Tables and Columns
Chapter 6 Working with Tables and ColumnsChapter 6 Working with Tables and Columns
Chapter 6 Working with Tables and Columns
 
Chapter 2 Getting Started with CSS
Chapter 2 Getting Started with CSSChapter 2 Getting Started with CSS
Chapter 2 Getting Started with CSS
 
Chapter 1 Getting Started with HTML5
Chapter 1 Getting Started with HTML5Chapter 1 Getting Started with HTML5
Chapter 1 Getting Started with HTML5
 
Integer overflows
Integer overflowsInteger overflows
Integer overflows
 
testing throughout-the-software-life-cycle-section-2
testing throughout-the-software-life-cycle-section-2testing throughout-the-software-life-cycle-section-2
testing throughout-the-software-life-cycle-section-2
 
Fundamental of testing
Fundamental of testingFundamental of testing
Fundamental of testing
 
Chapter 15 Risk Mitigation
Chapter 15 Risk MitigationChapter 15 Risk Mitigation
Chapter 15 Risk Mitigation
 
Chapter 14 Business Continuity
Chapter 14 Business ContinuityChapter 14 Business Continuity
Chapter 14 Business Continuity
 
Chapter 13 Vulnerability Assessment and Data Security
Chapter 13 Vulnerability Assessment and Data SecurityChapter 13 Vulnerability Assessment and Data Security
Chapter 13 Vulnerability Assessment and Data Security
 
Chapter 12 Access Management
Chapter 12 Access ManagementChapter 12 Access Management
Chapter 12 Access Management
 
Chapter 11 Authentication and Account Management
Chapter 11 Authentication and Account ManagementChapter 11 Authentication and Account Management
Chapter 11 Authentication and Account Management
 
Chapter 10 Mobile and Embedded Device Security
Chapter 10 Mobile and Embedded Device Security Chapter 10 Mobile and Embedded Device Security
Chapter 10 Mobile and Embedded Device Security
 
Chapter 9 Client and application Security
Chapter 9 Client and application SecurityChapter 9 Client and application Security
Chapter 9 Client and application Security
 
Chapter 8 Wireless Network Security
Chapter 8 Wireless Network SecurityChapter 8 Wireless Network Security
Chapter 8 Wireless Network Security
 
Chapter 7 Administering a Secure Network
Chapter 7 Administering a Secure Network Chapter 7 Administering a Secure Network
Chapter 7 Administering a Secure Network
 
Chapter 6Network Security Devices, Design, and Technology
Chapter 6Network Security Devices, Design, and TechnologyChapter 6Network Security Devices, Design, and Technology
Chapter 6Network Security Devices, Design, and Technology
 
Chapter 5 Networking and Server Attacks
Chapter 5 Networking and Server AttacksChapter 5 Networking and Server Attacks
Chapter 5 Networking and Server Attacks
 
Chapter 4 Advanced Cryptography and P K I
Chapter 4 Advanced Cryptography and P K IChapter 4 Advanced Cryptography and P K I
Chapter 4 Advanced Cryptography and P K I
 
Chapter 3 Basic Cryptography
Chapter 3 Basic CryptographyChapter 3 Basic Cryptography
Chapter 3 Basic Cryptography
 

Último

Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 

Último (20)

Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 

Chapter 11 Working with Events and Styles

  • 1. HTML5, CSS3, and JavaScript 6th Edition Working with Events and Styles Tutorial 11 Carey
  • 2. XPXPXPXPXPObjectives • Create an event handler • Reference an attribute from a page element • Change the inline style of a page element • Create code for mouse events • Create code for keyboard events New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 2
  • 3. XPXPXPXPXPObjectives (continued) • Design and apply custom cursors • Create and apply anonymous functions • Work with alert, confirm, and prompt dialog boxes New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 3
  • 4. XPXPXPXPXPIntroducing JavaScript Events • JavaScript programs run in response to events • Events: Actions initiated by the user or by the browser • Example: – Clicking an object on a form – Closing a web page • JavaScript events can be used to build a Hanjie puzzle, i.e., a grid in which each grid cell is either filled or left empty New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 4
  • 5. XPXPXPXPXPIntroducing JavaScript Events (continued) • The Hanjie puzzle app contains three puzzles named puzzle1, puzzle2, and puzzle3 New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 5
  • 6. XPXPXPXPXPCreating an Event Handler • Event handler: A property that controls how an object will respond to an event • Event handler waits until the event occurs and then responds by running a function or command block to execute an action New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 6
  • 7. XPXPXPXPXPCreating an Event Handler (continued 1) • Event handlers can be added to a page element using the following attribute: <element onevent = “script”> where element is the element in which the event occurs, event is the name of the event, and script are the commands that the browser runs in response to the event New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 7
  • 8. XPXPXPXPXPCreating an Event Handler (continued 2) • Event handlers can also be defined as object properties using the command object.onevent = function; where object is the object in which the event occurs, event is the name of the event, and function is the name of a function run in response to the event New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 8
  • 9. XPXPXPXPXPCreating an Event Handler (continued 3) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 9
  • 10. XPXPXPXPXPCreating an Event Handler (continued 4) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 10
  • 11. XPXPXPXPXPCreating an Event Handler (continued 5) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 11
  • 12. XPXPXPXPXPCreating an Event Handler (continued 6) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 12 • Add an event handler to the buttons in the Figure 11-8 • Apply the following onclick event handler to respond to a mouse click: object.onclick = function; where object is the page element that is being clicked and function is the function run in response to the click event
  • 13. XPXPXPXPXPCreating an Event Handler (continued 7) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 13
  • 14. XPXPXPXPXPCreating an Event Handler (continued 8) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 14 • The challenge lies in determining which button was clicked • There is no way of knowing which puzzle to load into the page without knowing which button activated the onclick event handler • This information can be determined using the event object
  • 15. XPXPXPXPXPUsing the Event Object New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 15 • Event object is an object that contains properties and methods associated with an event • Example: The action of clicking a mouse button generates an event object containing information such as which mouse button was clicked, where in the page it was clicked, the time at which it was clicked, and so forth
  • 16. XPXPXPXPXPUsing the Event Object (continued 1) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 16 • Event object is passed as an argument to the function handling the event to retrieve the information contained in the event object • Example: function myFunction(evt) { function code } where evt is the name assigned to the parameter receiving the event object from the event handler
  • 17. XPXPXPXPXPUsing the Event Object (continued 2) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 17
  • 18. XPXPXPXPXPUsing the Event Object (continued 3) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 18
  • 19. XPXPXPXPXPExploring Object Properties New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 19 • JavaScript object properties that mirror HTML attributes follow certain conventions • All JavaScript properties must begin with a lowercase letter • If the HTML attribute consists of multiple words, JavaScript property follows a format known as camel case, i.e., the initial word is in lowercase and the first letter of each subsequent word is in uppercase
  • 20. XPXPXPXPXPExploring Object Properties (continued) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 20 • If the name of the HTML attribute is a reserved JavaScript name or keyword, the corresponding JavaScript property is often prefaced with the text string html • class attribute is an exception to this convention because the class name is reserved by JavaScript for other purposes • References to the HTML class attribute use the className property
  • 21. XPXPXPXPXPObject Properties and Inline Styles New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 21 • Inline styles for each page element can be applied using the following style attribute: <element style = “property:value”> … where – element is the page element – property is a CSS style property – value is the value assigned to that property
  • 22. XPXPXPXPXP Object Properties and Inline Styles (continued) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 22 • The equivalent command in JavaScript is object.style.property = “value”; with the property style written in camel case • Inline styles have precedence over style sheets
  • 23. XPXPXPXPXP Creating Object Collections with CSS Selectors New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 23 • Change the background color of all table cells by applying the following CSS style rule for all td elements: table#hanjieGrid td { background-color: rgb(233, 207, 29); }
  • 24. XPXPXPXPXP Creating Object Collections with CSS Selectors (continued 1) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 24 • In JavaScript, to change the background color of all table cells, you must first define an object collection based on a CSS selector using the following querySelectorAll() method: document.querySelectorAll(selector) where selector is the CSS selector that the object collection is based on
  • 25. XPXPXPXPXP Creating Object Collections with CSS Selectors (continued 2) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 25 • Once the object collection has been defined, change the background-color style of each td element by applying the backgroundColor property to the objects in the object collection • Reference only the first element that matches a selector pattern using the following JavaScript method: document.querySelector(selector) where selector is a CSS selector
  • 26. XPXPXPXPXP Creating Object Collections with CSS Selectors (continued 3) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 26
  • 27. XPXPXPXPXP Creating Object Collections with CSS Selectors (continued 4) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 27
  • 28. XPXPXPXPXPWorking with Mouse Events New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 28 • JavaScript supports events associated with the mouse such as clicking, right-clicking, double- clicking, and moving the pointer over and out of page elements
  • 29. XPXPXPXPXPWorking with Mouse Events (continued 1) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 29 • A mouse action can be comprised of several events • The action of clicking the mouse button is comprised of three events, fired in the following order: – mousedown (the button is pressed down) – mouseup (the button is released) – click (the button has been pressed and released)
  • 30. XPXPXPXPXPWorking with Mouse Events (continued 2) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 30 • The event object for mouse events has a set of properties that can be used to give specific information about the state of the mouse
  • 31. XPXPXPXPXPWorking with Mouse Events (continued 3) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 31
  • 32. XPXPXPXPXPIntroducing the Event Model New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 32 • Event model: Describes how events and objects interact within the web page and web browser • The process in which a single event is applied to a hierarchy of objects is part of the event model
  • 33. XPXPXPXPXPIntroducing the Event Model (continued 1) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 33 • Once an event has been initiated, it propagates through the object hierarchy in three phases – Capture phase: The event moves down the object hierarchy starting from the root element (the browser window) and moving inward until it reaches the object that initiated the event – Target phase: The event has reached the target of the event object and no longer moves down the object hierarchy
  • 34. XPXPXPXPXPIntroducing the Event Model (continued 2) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 34 – Bubbling phase: The event propagates up the object hierarchy back to the root element (browser window) where the propagation stops
  • 35. XPXPXPXPXPIntroducing the Event Model (continued 3) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 35 • Limitations of event handlers – Event handlers, such as onclick and onmousedown, respond to events during the target phase, but they do not recognize the propagation of events through the capture and bubbling phases – Only one function can be applied to an event handler at a time
  • 36. XPXPXPXPXPAdding an Event Listener New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 36 • Event listener: Listens for events as they propagate through the capture, target, and bubble phases, allowing the script to respond to an event within any phase • Unlike event handlers, more than one function can be applied to an event using event listeners
  • 37. XPXPXPXPXPAdding an Event Listener (continued 1) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 37 • Add an event listener to an object by applying the addEventListener()method object.addEventListener(event, function [, capture = false]); where – object is the object in which the event occurs – event is the event – function is the function that is run in response to the event
  • 38. XPXPXPXPXPAdding an Event Listener (continued 2) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 38 – capture is an optional Boolean value • true indicates that the function is executed during the capture phase • false (the default) indicates that the function is run during the bubbling phase
  • 39. XPXPXPXPXPRemoving an Event Listener New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 39 • The event model allows to remove event listeners from the document by applying removeEventListener() method object.removeEventListener(event, function [, capture = false]); where object, event, function, and capture have the same meanings as the addEventListener() method
  • 40. XPXPXPXPXPRemoving an Event Listener (continued) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 40
  • 41. XPXPXPXPXPControlling Event Propagation New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 41 • The browser has its own default responses to events • Apply the following preventDefault() method to the event object to prevent the occurrence of the browser’s default actions: evt.preventDefault()
  • 42. XPXPXPXPXP Controlling Event Propagation (continued 1) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 42 • Alternatively, you can prevent the browser’s default action by returning the value false from the event handler function • The return false; statement does not prevent default actions if event listeners are used in place of event handlers
  • 43. XPXPXPXPXP Controlling Event Propagation (continued 2) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 43
  • 44. XPXPXPXPXPExploring Keyboard Events New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 44 • JavaScript supports the keydown, keypress, and keyup events that allow users to interact with the web page and browser through the keyboard
  • 45. XPXPXPXPXPExploring Keyboard Events (continued 1) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 45 • The keydown and keypress events are similar in name; the difference between them is as follows: – The keydown and keyup events are fired in response to the physical act of pressing the key down and of a key moving up when it is no longer held down – The keypress event is fired in response to the computer generating a character
  • 46. XPXPXPXPXPExploring Keyboard Events (continued 2) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 46
  • 47. XPXPXPXPXPExploring Keyboard Events (continued 3) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 47 • The value associated with a key event property is affected by the event itself • For the keypress event, the charCode, keyCode, and which properties all return a Unicode character number • Sample values of the charCode, keyCode, which, and key properties for different keyboard keys and events are shown in Figure 11-29
  • 48. XPXPXPXPXPExploring Keyboard Events (continued 4) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 48
  • 49. XPXPXPXPXPExploring Keyboard Events (continued 5) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 49 • Modifier keys: Alt, Ctrl, Shift, and Command keys • In addition to character keys, JavaScript supports the modifier keys through the use of the altKey, ctrlKey, shiftKey, and metaKey properties
  • 50. XPXPXPXPXPExploring Keyboard Events (continued 6) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 50
  • 51. XPXPXPXPXPChanging the Cursor Style New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 51 • Cursors can be defined using the following CSS cursor style: cursor: cursorTypes; where cursorTypes is a comma-separated list of cursor types
  • 52. XPXPXPXPXPChanging the Cursor Style (continued 1) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 52 • JavaScript command to define cursors is as follows: object.style.cursor = cursorTypes; where object is the page object that will display the cursor style when hovered over by the mouse pointer
  • 53. XPXPXPXPXPChanging the Cursor Style (continued 2) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 53 • Create a customized cursor from an image file using url(image)where image is an image file • Example: cursor: url(jpf_pencil.png), pointer;
  • 54. XPXPXPXPXPChanging the Cursor Style (continued 3) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 54 • By default, the click point for a cursor is located in the top-left corner of the cursor image at the coordinates (0, 0) • Specify a different location by adding the (x, y) coordinates of the click point to the cursor definition as follows: url(image) x y where x is the x-coordinate and y is the y- coordinate of the click point in pixels
  • 55. XPXPXPXPXPWorking with Functions as Objects New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 55 • Everything in JavaScript is an object, including functions • Anything that can be done with an object can be done with a function, including – Storing a function as variable – Storing a function as an object property
  • 56. XPXPXPXPXP Working with Functions as Objects (continued) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 56 – Using one function as a parameter in another function – Nesting one function within another function – Returning a function as the result of another function – Modifying the properties of a function
  • 57. XPXPXPXPXP Function Declarations and Function Operators New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 57 • The following hello() function is created using the function declaration format: function hello(){ alert(“Welcome to Hanjie!”);} • Function operator: The definition of the function becomes the variable’s “value” var hello = function (){ alert(“Welcome to Hanjie!”); }
  • 58. XPXPXPXPXP Function Declarations and Function Operators (continued 1) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 58 • The two ways of defining the hello() function differ in how they are stored – Functions defined with a function declaration are created and saved as the browser parses the code prior to the script being run • Since the function is already stored in memory, the statements that run the function can be placed prior to the statement that declares the function
  • 59. XPXPXPXPXP Function Declarations and Function Operators (continued 2) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 59 – Function operators are evaluated as they appear in the script after the code has been parsed by the browser • Function operators are more flexible than function declarations, allowing a function to be placed anywhere a variable can be placed
  • 60. XPXPXPXPXPAnonymous Functions New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 60 • Anonymous function has function declaration without the function name • Example: The following structure is an anonymous function: function() { commands } • An anonymous function can be inserted into any expression where a function reference is required
  • 61. XPXPXPXPXPAnonymous Functions (continued 1) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 61 • Functions that are named are called named functions • Anonymous functions are more concise and easier to manage because the function is directly included with the expression that invokes it • Anonymous functions limit the scope of the function to exactly where it is needed
  • 62. XPXPXPXPXPAnonymous Functions (continued 2) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 62
  • 63. XPXPXPXPXP Passing Variable Values into Anonymous Functions New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 63 • JavaScript supports two types of variables: – Global variables: Declared outside of any function and thus are accessible throughout the app – Local variables: Declared within a function and are only accessible to code within that function
  • 64. XPXPXPXPXP Passing Variable Values into Anonymous Functions (continued 1) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 64 • Global variables should be avoided when possible because – global variables are accessible to every function in the application – the task of tracking which functions are using and modifying the global variables becomes increasingly difficult as the application grows in size and complexity
  • 65. XPXPXPXPXP Passing Variable Values into Anonymous Functions (continued 2) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 65 • An advantage of using anonymous functions is that they reduce the need for global variables because they perform their actions locally within a function • One of the challenges of anonymous functions is keeping track of all of the nested levels of functions and procedures
  • 66. XPXPXPXPXP Passing Variable Values into Anonymous Functions (continued 3) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 66
  • 67. XPXPXPXPXPDisplaying Dialog Boxes New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 67 • Alert dialog box can be created using the following alert() method: alert(text) where text is the message displayed in the alert dialog box • When an alert dialog box is displayed, the execution of the program code halts until the user clicks the OK button in the dialog box
  • 68. XPXPXPXPXPDisplaying Dialog Boxes (continued 1) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 68 • JavaScript supports confirmation and prompt dialog boxes
  • 69. XPXPXPXPXPDisplaying Dialog Boxes (continued 2) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 69
  • 70. XPXPXPXPXPDisplaying Dialog Boxes (continued 3) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 70