Lect4 JS2
Lect4 JS2
JavaScript: Part 2
Introduction
Contents
Document object
Forms
<input> tag
passwords,
checkboxes,
radio buttons, and
the special buttons Submit and Reset
type = text
type = password
10
type = checkbox
11
Example
Grocery Checklist
<form action = ""> <p>
<input type = "checkbox" name = "groceries"
value = "milk" checked = "checked"> Milk <br/>
<input type = "checkbox" name = "groceries"
value = "bread"> Bread <br/>
<input type = "checkbox" name = "groceries"
value= "eggs"> Eggs
</p> </form>
12
type = radio
13
Example
Age Category
<form action = ""> <p>
<input type = "radio" name = "age"
value = "under20" checked = "checked"> 0-19 <br/>
<input type = "radio" name = "age"
value = "20-35"> 20-35 <br/>
<input type = "radio" name = "age"
value = "36-50"> 36-50 <br/>
<input type = "radio" name = "age"
value = "over50"> Over 50 </p>
</form>
14
<select> tag
15
Example
Grocery Menu - milk, bread, eggs, cheese
<form action = "">
<p>
With size = 1 (the default)
<select name = "groceries">
</form>
16
Example
17
<textarea> tag
18
Example
Please provide your employment aspirations
<form action = "">
<p>
<textarea name = "aspirations" rows = "3 cols = "40">
(Be brief and concise)
</textarea>
</p>
</form>
19
21
create documents,
22
24
25
<html> <head></head>
<body>
<form action = "">
<input type = "button" name = "pushMe">
</form>
</body>
</html>
<body>
<form name = "myForm" action = "">
<input type = "button" name = turnIton>
</form>
</body>
Defined in DOM 1
document.getElementById(turnItOn")
29
31
Event-Driven Programming
32
33
Event Registration
tag attributes
assigns
34
Tag Attribute
onabort
onblur
onchange
onclick
onerror
onfocus
onload
onmouseout
onmouseover
onreset
onreszie
onselect
onsubmit
onload
35
Tag
Description
onblur
<a>
<button>
<input>
onchange
<select>
<input>
<textarea>
<select>
Tag
Description
onclick
<a>
<input>
<a>
<input>
onfocus
...
38
39
OK
SHOW load.html
40
Plain Buttons
<input type = button name = freeOffer
id = freeButton />
Radio buttons
A simple and effective way to collect multiple-choice
input
<input type = "radio" name = "button_group"
value = "blue" onclick = "handler()">
Cant use the elements name to identify it, because
all buttons in the group have the same name
42
Radio buttons
Must use the DOM address of the element
Ex) var rElement = document.myForm.elements;
SHOW radio_click.html
43
Event handlers
Event handlers can be specified by assigning them to
properties of the JavaScript objects associated with
the HTML elements
This sets the handler for the first element in the form
44
Event Handlers
SHOW radio_click2.html
45
blur
focus
change
select
47
Blur Event
48
50
51
Example 1
To keep the form active after the event handler is
finished, have it return false
The form just has two password input boxes to get the
passwords and Reset and Submit buttons
52
Example 1
Handler actions:
If the two passwords are not the same, alert it, focus
and select the first box and return false if they are the
same, return true
53
Display of pswd_chk.html
Password Input
Your password
Verify password
Reset
Submit Query
Password Input
Your password
Verify password
Reset
Submit Query
OK
Example 2
(Ex) Checking the format of a name and phone number
55
Figures
Customer Information
Heel, Ferris, W.
999-555-333
Reset
Submit Query
Customer Information
Heel, Ferris, W.
999-555-333
Reset
Submit Query
The phone number you entered (999-555-333) is not in the correct form.
The correct form is: ddd-ddd-dddd
Please go back and fix your phone number
OK
56
Name Format
57
58
Event propagation
61
62
OK
OK
64