...

View Full Version : 'Code' is undefined error



RonMcIntire
01-17-2009, 07:14 PM
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>

Philip M
01-17-2009, 08:50 PM
<script language="text/javascript"> :eek::confused:

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

RonMcIntire
01-17-2009, 10:19 PM
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

Philip M
01-17-2009, 10:59 PM
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.

Cubefreak101
01-17-2009, 11:05 PM
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>

RonMcIntire
01-18-2009, 04:54 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum