PDA

View Full Version : Form Object and Undefined Variables


rwarcup
08-09-2002, 05:06 PM
Hello,

I'm developing a RAG (Red/Amber/Green) status control. See code below.

I get an 'Error 'result_rag' is undefined when I include the '<IMG id=result_rag SRC="/images/green.gif" title="Green">' code inside <FORM></FORM> tags. When there are no <FORM> tags the code & control works ok.

Is this because I need to refer to the FORM object as part of the result_rag variable? How do I do this?

Any help would be appreciated.

Thanks in advance
Regards
Rob

<HTML>
<HEAD>
<title>RAG Status</title>
<script Language="JavaScript">

// build array
var List = new Array();
List[0] = new Array();
List[1] = new Array();
List[2] = new Array();

// Populate the array
List[0][0] = "Green";
List[0][1] = "/images/green.gif";

List[1][0] = "Amber";
List[1][1] = "/images/amber.gif";

List[2][0] = "Red";
List[2][1] = "/images/red.gif";

function call(rag){
comment1.innerText=List[rag][0];
result_rag.title=List[rag][0];
result_rag.src=List[rag][1];
}
</script>
</HEAD>
<BODY class=bdy>

<FORM NAME="aform" METHOD="POST" ACTION="store.asp">
<TABLE WIDTH="100%" BORDER="0" valign=top class=normaltext>
<tr valign="top">
<td width="20%">
<table border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=20 onclick="call(0)" style="CURSOR: hand" title="Red"><IMG SRC="/images/green.gif"></td>
<td width=20 onclick="call(1)" style="CURSOR: hand" title="Amber"><IMG SRC="/images/amber.gif"></td>
<td width=20 onclick="call(2)" style="CURSOR: hand" title="Green"><IMG SRC="/images/red.gif"></td>
<td width=20>&nbsp;</td>
<td width=20><IMG id=result_rag SRC="/images/green.gif" title="Green"></td>
</tr>
</table>
<span id="comment1">Green</span>
</td>
</tr>
</table>
</FORM>

</BODY>
</HTML>

Exodious
08-09-2002, 05:20 PM
Well, there are a few ways to sort it, you could either make the images part of the form and name the form - then change your function like this

function call(rag){
var result_rag = document.aform.result_tag.src; //.value or .src
comment1.innerText=List[rag][0];
result_rag.src=List[rag][0];
result_rag.src=List[rag][1];
}

The form image element
<input type="image" src="/images/green.gif">

Or, you could just add document.all to your function references

so

This one is I.E only

function call(rag){
comment1.innerText=List[rag][0];
document.all.result_rag.src=List[rag][0];
document.all.result_rag.src=List[rag][1];
}

:thumbsup:

requestcode
08-09-2002, 05:21 PM
On the img tag change "id" to "name" and then change your script to this:
function call(rag){
comment1.innerText=List[rag][0];
document.result_rag.title=List[rag][0];
document.result_rag.src=List[rag][1];
}

Exodious
08-09-2002, 05:24 PM
Originally posted by requestcode
On the img tag change "id" to "name" and then change your document.result_rag
}

of course he meant "document.all.result_rag"

requestcode
08-09-2002, 08:18 PM
Nope I meant
document.result_rag.title=List[rag][0];
document.result_rag.src=List[rag][1];

The "all" is not needed.

rwarcup
08-09-2002, 08:48 PM
Thanks very much - you've saved some of my sanity! Each of these options work.

Regards
Rob