Javascript Select Case
2003-03-16 12:19:57
Category: javascript:general
Description: Example on using switch to select a case.
Author: mind
Viewed: 61438
Rating: (161 votes)


<!-- switch.js - mind (mind@metalshell.com) -->
 
<!-- example on how to use switch -->
<!-- $ If you have any problems or questions, email me $ -->
<!-- http://www.metalshell.com -->
 
<!-- put this code into the <head> section of your html document. -->
 
<!-- start our javascript -->
<script language=JavaScript>
function swExmple()
{
  swExmpl(2);
  swExmpl(1);
  swExmpl(3);
  swExmpl("www.metalshell.com");
}
 
function swExmpl(ex)
{
  switch (ex)
  {
    case 1: { document.write('<font>Caught 1</font><br>'); break }
    case 2: { document.write('<font>Caught 2</font><br>'); break }
    // And so on.. and so on..
 
    // whatever is passed to switch that is not caught
    // by the above fall under this category.
    default: { document.write('<font>Error: ' + ex + '</font><br>'); break }
  }
}
 
// end of our javascript
</script>
<!-- place this code anywhere on your html document (example) -->
 
<!-- align our link below -->
<div align=center>
        <!-- make a link that points to our js function printText() -->
        <a href="javascript: swExmple();">Click Here!</a>
</div>