conditions and more web form fields
DESCRIPTION
Conditions and More Web Form Fields. Conditions. if (then) else Syntax. if. (. ). Boolean expression. {. instructions if expression is true (then). }. else. {. instructions if expression is false. }. Example. Check age. < 59. >=59 and 65. Gotta Work!. Early Retirement?. - PowerPoint PPT PresentationTRANSCRIPT
Conditions and More Web Form Fields
Conditions
if (then) else Syntax
if ( )Boolean expression
{
{
}
}
instructions if expression is true (then)
instructions if expression is false
else
Example
Check age
< 59 >65
Gotta Work! Retire!
>=59 and <65
Early Retirement?
Example – Step 1if ( )age < 59{
}alert(‘Gotta Work!’)
Example – Step 2if ( )age < 59{
{
}
}alert(‘Gotta Work!’)
else
{
}alert(‘Early Retirement?’)
if ( )age >= 59 && age < 65(age >= 59) && (age < 65)
Example – Step 2 (v2)if ( )age < 59{
}alert(‘Gotta Work!’)
elseif ( )(age >= 59) && (age < 65){
}alert(‘Early Retirement?’)
Example – Step 3if ( )age < 59{
}alert(‘Gotta Work!’)
elseif ( )(age >= 59) && (age < 65){
}alert(‘Early Retirement?’)
else{
}alert(‘Retire!’)
Example – Step 3 (v2)if ( )age < 59{
}alert(‘Gotta Work!’)
elseif ( )(age >= 59) && (age < 65){
}alert(‘Early Retirement?’)
else{
}alert(‘Retire!’)
Relational Operators
== Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
!= Not equal to
Danger Will Robinsonfunction check(p){
var a = palert('initial a = ' + a)if (a = 5){
alert('if a = 5')}alert('final a = ' + a)
}
check(10)initial a = 10
if a = 5final a = 5
a == 5JavaScript assignment statements execute and evaluate to true.
Expressions and Conditions
if ( )(age >= 59) && (age < 65)
Boolean Expression
Condition 1 Condition 2
AND and OR
AND &&Both conditions true expression is true.Otherwise expression is false.
if (condition1 && condition2)
OR ||Either condition true expression is true.
if (condition1 || condition2)
More Web Form Fields
Checkbox
<input type="checkbox" name="student" checked="checked" />
Checkbox
document.getstuff.student.checked
. . .document form field checked
Booleantrue orfalse
Radio Button
<input type="radio" name="sex" Id="female"value="F" />
<input type="radio" name="sex" id="male"value="M" />
Gender:Male <input type="radio" name="sex" id="male" value="M" /> Female <input type="radio" name="sex" id="female" value="F" />
Index = 0
Index = 1
var messagevar indx
message = ‘by Index using Checked property\n‘
for (indx = 0; indx < document.getstuff.sex.length; indx++){
message = message + 'Indx=' + indx + ' checked=' + document.getstuff.sex[indx].checked + '\n‘
}
alert(message)
message = ‘by Index using Value property\n‘
for (indx = 0; indx < document.getstuff.sex.length; indx++){
message = message + 'Indx=' + indx + ' value=' + document.getstuff.sex[indx].value + '\n‘
}
alert(message)
message = ‘by d.f.id.checked\n‘
message = message + 'document.getstuff.male.checked=' + document.getstuff.male.checked + '\n‘
message = message + 'document.getstuff.female.checked=' + document.getstuff.female.checked + '\n‘
alert(message)
Select and Option
<select name="major"><option value="mis">Management Information Systems</option><option value="cis">Computer Information Systems</option><option value="isp">Information Security and Privacy</option>
</select>
Select and Option
document.getstuff.major.value
. .document form field value.
<select name="major"><option value="mis">Management Information Systems</option><option value="cis">Computer Information Systems</option><option value="isp">Information Security and Privacy</option>
</select>