Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Function Not Defined???

    Hello All,

    I am new to Javascript and am trying to understand it. I wrote a simple little script that displays a menu on the screen and then prompts the user for a choice, after they choose the selection is sent to a function which contains switch case and then the result is returned and displayed on the screen.

    When I run the script I get an error (javascript console) that tells me my function is not defined? It is in the header, shouldn't it be defined? Any help identifying my error would be appreciated, I don't want to move on until I understand fully.

    Here is the code:

    <code>
    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <script language="javascript" type="text/javascript">

    <!--

    function theChoice(whichMenu) {
    switch(whichMenu)
    {
    case="a":
    return "Japanese";
    break;

    case="b":
    return "Mexican";
    break;

    case="c":
    return "Indian";
    break;

    case="d":
    return "American";
    break;

    default:
    return "Not a Choice";
    break;
    }
    }

    //-->

    </script>

    </head>

    <body>
    <table align="center" width="500" cellspacing="5" cellpadding="5" border="0">
    <th align="left">SELECT YOUR MENU CHOICE:</th>
    <tr>
    <td>a) Japanese</td>
    </tr>
    <tr>
    <td>b) Mexican (legal)</td>
    </tr>
    <tr>
    <td>c) Indian (not Native American)</td>
    </tr>
    <tr>
    <td>d) American </td>
    </tr>
    </table>

    <script language="javascript" type="text/javascript">
    <!--
    var menuType=prompt("Enter a Letter (a, b, c or d)", "");

    var whichMenu=theChoice(menuType);

    document.write("<br> The" +whichMenu+ "menu will be brought to your table");

    //-->
    </script>
    </body>
    </html>
    </code>

    Thank You!

    John

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,032
    Thanks
    0
    Thanked 248 Times in 245 Posts
    You have a syntax error.

    Change this:
    Code:
    case="a":
    return "Japanese";
    break;
    to:
    Code:
    case "a":
    return "Japanese";
    break is unnecessary in this case (though switch normally needs it) as you already do a return which will cause the next statement not to be executed.

  • #3
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thank You!

    Thank you for your help,

    I only needed to tweek the code a bit further, it still gave me a syntax error until I moved the : in front of the case.

    Code:
    case "a":
    return "Japanese";
    Last edited by jrcoleman; 06-19-2007 at 07:14 PM. Reason: code not entered correctly

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,032
    Thanks
    0
    Thanked 248 Times in 245 Posts
    Oopps, I didn't realize I did that.
    Glad you fixed it yourself.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •