Wednesday, March 5, 2014

JavaScript If..... Else statements

The if statement is executed only when the specified condition is the true, if the first if condition is false then control is shifted to else if and execute it, if the else if condition is false then control is shifted to the else condition and execute it.

Syntax:

if(condition){//start of if body
      statement 1... executed if the condition is true
}//end of if body
else if(condition) {//start of if body
      statement 1... executed if the condition is true
}//start of if body
else {//start of else body
       statement 1... executed if the condition is true
}//start of else body

Demo:
Enter Day Name:

Code:
<!DOCTYPE html>
<head>
<script>
function WorkTime(){
var dayVal = document.getElementById("dayName").value;
if(dayVal.toUpperCase()=="MONDAY") {
alert("OOps! first day at office...");
}
else if(dayVal.toUpperCase()=="TUESDAY") {
alert("OOps! 2nd day at office...");
}
else if(dayVal.toUpperCase()=="WEDNESDAY") {
alert("OOps! 3rd day at office...");
}
else if(dayVal.toUpperCase()=="THURSDAY") {
alert("OOps! 4th day at office...");
}
else if(dayVal.toUpperCase()=="FRIDAY") {
alert("OOps! Tomorrow holiday enjoying in office");
}
else if(dayVal.toUpperCase()=="SATURDAY") {
alert("Enjoying Saturday at Home");
}
else if(dayVal.toUpperCase()=="SUNDAY") {
alert("Sleep time");
}
else {
alert("This is not a valid day...");
}
}
</script>
</head>
<body>
<table>
<tr>
<td>Enter Day Name:</td>
<td><input type="text" id="dayName"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Hit" onClick="WorkTime()"><td>
</tr>
</table>
</body>
</html>

No comments:

Post a Comment