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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    'Code' is undefined error

    I am trying to write a snippet to put in my form and cannot get it to run. Can anyone see what's wrong? I keep getting an undefined error for the variable 'Code' no matter what I do.

    The goal is present obtain a Time Frame based on an SVCode selected from a group of radio buttons.

    Thanks. Ron

    <html>
    <head>
    <script language="text/javascript">
    var SV01="";
    var SV02="";
    var SV03="";
    var SV04="";
    var DelTime="test";
    var code="SV03";

    function getDelTime(code)
    {
    if (code==SV01) { DelTime="8A-5P: 8 Hour Window" }
    else
    if (code==SV02) { DelTime="8A-8P: 4 Hour Window" }
    else
    if (code==SV03) { DelTime="8A-8P: 2 Hour Window" }
    else
    if (code==SV04) { DelTime="8A-8P: 1 Hour Window" }
    return (DelTime);
    }
    </script>

    </head>

    <body>

    <form>
    <tr>
    <td
    valign="top" align="left" bgcolor="#FFCCFF" colspan="2" width="143" height="22">
    <p align="left"><em style="font-style: normal; font-weight:700"><font color="#FF0000">*</font>
    Time Frame:</em>
    </td>
    <td
    valign="top" align="left" bgcolor="#FFCCFF" colspan="2" width="609" height="22">
    <p style="margin-top: 0; margin-bottom: 0"><b>Select a type of Valentine:</b></p>
    <p style="margin-top: 0; margin-bottom: 0">
    <input type="radio" name="DelCode" value="SV01" LANGUAGE="javascript" DelTime="getDelTime(DelCode)" document.write(code);> 8A-5P: 8 Hour Window ($50)</p>
    <p style="margin-top: 0; margin-bottom: 0">
    <input type="radio" name="DelCode" value="SV02" LANGUAGE="javascript" DelTime="getDelTime(DelCode)"> 8A-8P: 4 Hour Window ($60)</p>
    <p style="margin-top: 0; margin-bottom: 0">
    <input type="radio" name="DelCode" value="SV03" LANGUAGE="javascript" DelTime="getDelTime(DelCode)"> 8A-8P: 2 Hour Window ($75)</p>
    <p style="margin-top: 0; margin-bottom: 0">
    <input type="radio" name="DelCode" value="SV04" LANGUAGE="javascript" DelTime="getDelTime(DelCode)"> 8A-8P: 1 Hour Window ($95)</p>
    &nbsp;
    </td>
    </tr>

    <input type="button"
    onclick=document.write(code);
    value="Call function">

    </form>

    </body>

    </html>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    <script language="text/javascript">

    Change to:-

    <script type ="text/javascript">


    Your radio buttons are a mess. How does LANGUAGE="javascript" get into it? They should read something like:-

    <input type="radio" name="DelCode" value="SV01" onclick = "getDelTime('SV01')" > 8A-5P: 8 Hour Window ($50)</p>

    Note that the document.write command must be carried out during the loading of the page. So, if it is attached to any event that executes after the page has loaded, then the whole page will be replaced with the contents of the document.write command.



    The whole aim of practical politics is to keep the populace alarmed (and hence clamorous to be led to safety) by menacing it with an endless series of hobgoblins, all of them imaginary.
    H.L. Mencken 1880-1956, American Editor, Author, Critic, Humorist
    Last edited by Philip M; 01-17-2009 at 08:00 PM.

  • #3
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Philip:

    That's exactily what I needed. However, there's still a glitch somewhere.

    It works as long as I have a value assigned to the variable "Code." I assigned the value "SV03" to "Code" to try to make the routine work but "Code" can have any of four values: SV01, SV02, SV03 or SV04. When I took out the value or assign a "", the error returned. Any suggestions?

    The reason for the document.write statement was that I wanted to know what value the variable "DelTime". Is there a better way?

    COMMENT: I'm learning that javascript tutorials on the web are okay to a point, but trying to apply their simplified coding examples sometimes leaves a lot to be desired.

    Ron

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by RonMcIntire View Post
    Philip:

    That's exactily what I needed. However, there's still a glitch somewhere.

    It works as long as I have a value assigned to the variable "Code." I assigned the value "SV03" to "Code" to try to make the routine work but "Code" can have any of four values: SV01, SV02, SV03 or SV04. When I took out the value or assign a "", the error returned. Any suggestions?
    You should pass the value of code to the function as I have indicated above.

    <input type="radio" name="DelCode" value="SV01" onclick = "getDelTime('SV01')" >

    The global variable code in var code="SV03"; is not the same variable as the local variable defined in function getDelTime(code) .



    The reason for the document.write statement was that I wanted to know what value the variable "DelTime". Is there a better way?
    Use alert() for testing purposes. Or create a <div> and use innerHTML.

    COMMENT: I'm learning that javascript tutorials on the web are okay to a point, but trying to apply their simplified coding examples sometimes leaves a lot to be desired.
    Ron
    Well, you would not expect to be competent and fluent in other subjects just by looking at tutorials on the web, would you? That is like learning to swim by correspondence course.
    Last edited by Philip M; 01-17-2009 at 10:23 PM. Reason: typo

  • #5
    New Coder
    Join Date
    Oct 2008
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Well for debug purposes, I always use alert() to return a value so I can see it. Or if you're set on writing it to text on the page, set up a div and use innerHTML, like so:

    <html>
    <head>
    <script type="text/javascript">
    var exampleTxt = "Hello, world!";

    function writeVar() {
    document.getElementById('myDiv').innerHTML = exampleTxt
    }
    </script>
    </head>

    <body>
    <div id="myDiv"><!-- content goes here --><br /></div><br />
    <input type="button" value="innerHTML Demo" onClick="writeVar()" />
    </body>
    </html>

  • #6
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you both for your response. It all makes sense now. I also realize that in my function, getDelTime, the statement if (code==SV01) should have been if(code=="SV01"). Once I made the change, everything began to work as it should.

    Using alert() to check values was a good idea. I may have accidentally used that technique but forgot it.

    Ron


  •  

    Posting Permissions

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