Lecture 7 (B) JavaScript DOM
Lecture 7 (B) JavaScript DOM
JavaScript DOM
1
Summary of the previous lecture
• What is JavaScript?
• Embedding JavaScript with HTML
• JavaScript conventions
• Variables in JavaScript
• JavaScript operators
• Input output in JavaScript
• JavaScript functions
• Conditional Statements
• Looping Statements
2
Outline
• Dialog boxes in JavaScript
• HTML Document Object Model (DOM)
3
1. Dialog boxes in JavaScript
• JavaScript provides the ability to pickup user
input or display small amounts of text to
the user by using dialog boxes
• These dialog boxes appear as separate windows
and their content depends on the information
provided by the user
1.1 Alert Box
• An alert box is simply a small message box that
pops up and gives the user some information
• An alert dialog box is mostly used to give a
warning message to the users
• When an alert box pops up, the user will have to
click "OK" to proceed
• Syntax:
– alert(“message”)
1.1 Alert Box…
Prompt box
User input
confirmation
Not confirmed
2. Document Object Model (DOM)
• Once html element are rendered (painted) in
the browser window, browser can not
recognize them
• To create interactive web pages it is vital that
the browser continues to recognize HTML
elements
• JavaScript enabled browsers do this
because they recognize and uses DOM
11
2. Document Object Model (DOM)…
13
2. Document Object Model (DOM)…
Window
select list
s buttons
Textboxe
of forms
element
Array of
Reset
li
files
14
2. Document Object Model (DOM)…
• document.forms[0].elements[0].value
• document.images[0].src
• document.links[0].href
2.1 Retrieving HTML elements
• The getElementById() method is a workhorse
method of the DOM
• It retrieves a specified element of the HTML
document and returns a reference to it
• To retrieve an element, it must have an
unique id
– document.getElementById(“element-id”)
16
2.1 Retrieving HTML elements…
• The returned reference can be used to
retrieve element attributes
– document.getElementById(“element
- id”).attribute
– document.getElementById(“pic”).src
– document.getElementById(“pic”).hei
ght
17
2.1 Retrieving HTML elements…
Function starts
Getting
reference to pic
Accessing
Attribute
s
Setting id
Image tag
Calling function
18
2.2 Retrieving the text of an element
19
2.2 Retrieving the text of an element
Getting reference to
element
Getting text
Paragraph text
20
2.3 Getting value of attributes
• getAttribute() method is used to retrieve values
of attributes
– document.getElementById(“element
- id”).getAttribute(“attribute-name”)
– document.getElementById(“pic”).get
Attribute(“src”)
21
2.3 Getting value of attributes
Getting reference to
element
22
2.4 Setting value of attributes
• setAttribute() method is used to set values of
attributes
– document.getElementById(“element-
id”).setAttribute(“attribute-name”, ”Value”)
– document.getElementById(“pic”).setAttribute(“src”
, ”abc.jpg”)
23
2.4 Setting value of attributes
Changing the
value of attribute
24
Summary
• Dialog boxes in JavaScript
• HTML DOM
• Retrieving HTML elements
• Setting HTML elements
25
Reference
s • Chapter 11. Beginning HTML,
XHTML,CSS, and JavaScript, by Jon
Duckett, Wiley Publishing; 2009, ISBN:
978-0-470-54070-1.
• Chapter 3,6, Learn JavaScript, by Chuck
Easttom, Wordware Publishing; 2002,
ISBN 1-55622-856-2