Geeks With Blogs
Geekette Mai Blog

Below the HTML sets up the page for multi-level conditionals

<!DOCTYPE html PUBLIC "_//W3C/DTD XHTML 1.0   Transitional//EN "

   "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title>Switch/Case Handling</title>

   <Script  type= "type/javarscript"  src="Script09.js"></script>

</head>

<body  bgcolor= "#FFFFFF"

<h2>Famous  quote on the President day!!</h2>

// by the way today is the President day 02/15/2010

<form action= " # ">

  <input type= "button" id= "Barah Obama" Value = "Barah Obama"

  <input type= "button" id= "George W Bush"  Value = "George W Bush"

  <input type= "button" id= "Bill Clinton" Value = "Bill Clinton"

</form>

</body>

</html>

NOW is the time for us to use a Switch/Case statment

1.  Window.onload = initAll;

When on the pageLoads, call the initAll ( ) function

 

2. function initAll ( ) {

document.getElemtbyId ("Barah Obama").onclick = the new president of the United States;

document.getElemtbyId ("George W Bush").onclick = the former president of the Untined States;

document.getElemtbyId ("Bil lClinton").onclick = the previous former president of the Untined States;

 

In the function, we set onClick handler for each of the button on the page.   Because we set id attribute along with the value attribute on HTML, we've able to use getElementById ( ) to set event handler.   If it existed, it would have been nice to be able to use  a getelementByValue ( ) Call --then, we wouldn't had  to set the id attribute.

 

3. Function   saysomething ( ) {

This begins the saysomething ( ) function.

 

4.switch(this.id) {

The id of this object is used as the parameter to switch( ). Its value will decide which of the below case statements gets executed.

5 Case " Barah Obama";

alert("Yes We Can" );

break;

If the Id of this object  is  " Barah Obama", this alert appears.   Regarding break, if the user clicked Barah Obama, we've in this section of code.  However, we've done everything we want to do, and we want to get out of the switch.   In order to do that, we need to break out.   Otherwise, we'll execute all of the code below, too.   While that continued execution cna be handly in certain circumtances, this isn't one of them.

6 Case "Geoge W. Bush";

alert(" You mess with Bush, you messed with Texas.");

break;

7  Case "Bill Clinton";

alert("I didn't have a sexual relationship with that woman");

break;

And finally, if user click on George Bush we end up poping up another alert and then breaking out of the switch.

8 Default:

If you were wondering what would happen if the user's entry didn't meet on of the critiria, you are in the right place.   The defaul section is where we end up our switch value didn't match any of the case values.   The default block is optional, but it's always good coding practice to include it. just incase (so to speak).  In this script, there no code their to execute, because we shouldn't get over here.

9 }

This closing brace ends the switch statement

 

 

 

Posted on Monday, February 15, 2010 12:17 PM | Back to top


Comments on this post: Using Multi-level Conditionals in a Switch/Case statement

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Mai Nguyen | Powered by: GeeksWithBlogs.net