07DOM and JS Events
07DOM and JS Events
07DOM and JS Events
JavaScript
is
almost
always
used
to
interact
with
the
HTML
document
in
which
it
is
contained.
This
is
accomplished
through
a
programming
interface
(API)
called
the
Document
Object
Model
(DOM).
DOM
is
a
live
structure.
DOM
is
a
tree
data
structure,
each
node
may
refer
to
other
nodes,
children,
and
so
on.
There
are
• Document
node,
• documentElement
node,
• element
nodes,
• text
nodes,
• aPribute
nodes,
• comment
nodes.
Property Descrip;on
Method
Descrip;on
createA<ribute()
Creates
an
aPribute
node
var
text
=
latest.textContent;
//access
the
text
Modifying a DOM element
More verbosely, and validated
Property Descrip;on
className The current value for the class aPribute of this HTML element.
innerHTML
Represents
all
the
things
inside
of
the
tags.
This
can
be
read
or
wriPen
to
and
is
the
primary
way
which
we
update
parNcular
div's
using
JS.
style
The
style
aPribute
of
an
element.
We
can
read
and
modify
this
property.
tagName
The
tag
name
for
the
element.
Attributes
APributes
are
only
available
on
element
nodes,
and
can
be
accessed
by
methods
such
as:
getAttribute(name)
removeAttribute(name)
setAttribute(name,val)
We
can
add
or
remove
any
style
using
the
style
or
className
property
of
the
Element
node.
var
commentTag
=
document.getElementById("specificTag");
commentTag.style.backgroundColour
=
"#FFFF00";
commentTag.style.borderWidth="3px";
Element.style
get
access
to
CSS2ProperNes
object
border-‐top-‐color
à
borderTopColor
Changing an element’s style
With class
Screen:
to
inspect
the
properNes
of
the
screen
on
which
the
current
window
is
being
rendered
Section 7 of 8
JAVASCRIPT EVENTS
JavaScript Events
A
JavaScript
event
is
an
acNon
that
can
be
detected
by
JavaScript.
We
say
then
that
an
event
is
triggered
and
then
it
can
be
caught
by
JavaScript
funcNons,
which
then
do
something
in
response.
JavaScript Events
For
example,
if
you
wanted
an
alert
to
pop-‐up
when
clicking
a
<div>
you
might
program:
<div
id="example1"
onclick="alert('hello')">Click
for
pop-‐up</div>
The
problem
with
this
type
of
programming
is
that
the
HTML
markup
and
the
corresponding
JavaScript
logic
are
woven
together.
It
does
not
make
use
of
layers;
that
is,
it
does
not
separate
content
from
behavior.
Listener Approach
Two ways to set up listeners
Listener Approach
Using functions
An
alternaNve
to
that
shown
in
LisNng
6.12
is
to
use
an
anonymous
funcNon
(that
is,
one
without
a
name),
as
shown
in
LisNng
6.13.
Event Object
No
maPer
which
type
of
event
we
encounter,
they
are
all
DOM
event
objects
and
the
event
handlers
associated
with
them
can
access
and
manipulate
them.
Typically
we
see
the
events
passed
to
the
funcNon
handler
as
a
parameter
named
e.
function
doSomething(e)
{
//
e
is
the
event
that
triggered
this
handler.
if
(!e)
var
e
=
window.event;
//
this
refers
to
the
HTML
element
which
currently
handles
the
event
//
e.target
or
e.
srcElement
//refer
to
the
HTML
element
the
event
originally
took
place
on
}
Event Object
element2.addEventListener('click',doSomething,false);
function doSomething(e){
There
are
several
classes
of
event,
with
several
types
of
event
within
each
class
specified
by
the
W3C:
• mouse
events
• keyboard
events
• form events
• frame events
Mouse events
Event
Descrip;on
onclick
The
mouse
was
clicked
on
an
element
ondblclick
The
mouse
was
double
clicked
on
an
element
onmousedown
The
mouse
was
pressed
down
over
an
element
onmouseup
The
mouse
was
released
over
an
element
onmouseover
The
mouse
was
moved
(not
clicked)
over
an
element
onmouseout
The
mouse
was
moved
off
of
an
element
onmousemove
The
mouse
was
moved
while
over
an
element
Keyboard events
Event
Descrip;on
onkeyup
The
user
releases
a
key
that
was
down
(this
happens
last)
Keyboard events
Example
Frame Events
window.onload=
funcNon(){
//all
JavaScript
iniAalizaAon
here.
}
Frame Events
Table of frame events
Event Descrip;on
onchange
Some
<input>,
<textarea>
or
<select>
field
had
their
value
change.
This
could
mean
the
user
typed
something,
or
selected
a
new
choice.
onfocus
ComplemenNng
the
onblur
event,
this
is
triggered
when
an
element
gets
focus
(the
user
clicks
in
the
field
or
tabs
to
it)
onreset
HTML
forms
have
the
ability
to
be
reset.
This
event
is
triggered
when
that
happens.
onselect
When
the
users
selects
some
text.
This
is
oien
used
to
try
and
prevent
copy/paste.
onsubmit
When
the
form
is
submiPed
this
event
is
triggered.
We
can
do
some
pre-‐validaNon
when
the
user
submits
the
form
in
JavaScript
before
sending
the
data
on
to
the
server.
Focus and Blur Events
Validating Forms
You mean pre-validating right?
if
(inputField.checked)
//Now
we
know
the
box
is
checked
}
Validating Forms
Number Validation
Submitting Forms
formExample.submit();
This
is
oien
done
in
conjuncNon
with
calling
preventDefault()
on
the
onsubmit
event.
What you Learned
1 What is
JavaScript 2 JavaScript
Design
3 Using
JavaScript 4 Syntax
5 JavaScript
Objects 6 The DOM
7 JavaScript
Events 8 Forms