Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object error

Status
Not open for further replies.

rjsaul0

IS-IT--Management
Mar 2, 2006
6
Hi,

In the example below, the reference to document.form1.vcol.value works in the pick function but error says not defined in the crand function. Using IE 7.

Rick

<html>
<Script type="text/javascript">
<!--
function crand () {
var rnum = Math.random();
var rc;
if (rnum < 0.5)
{rc = "red";}
else
{rc = "blue";}
document.form1.vcol.value=rc;
return rc;
}
function pick(c) {
document.form1.result.value=c;
document.form1.vcol.value=c;
// window.alert(c);
return;
}

var rcol = crand();

// hide -->
</Script>

<Style type='text/css'>
<!--
#ButtonR{
background-color:red;
color:black;
font-size:medium
}
#ButtonB{
background-color:blue;
color:black;
font-size:medium
}
-->
</Style>

<body>

<h2>Guess color, red or blue</h2>
<form name=form1>
&nbsp;
<input id='ButtonR' type="button" name=button1 value="Click" onClick="pick('red',rcol)">
&nbsp;
<input id='ButtonB' type="button" name=button2 value="Click" onClick="pick('blue',rcol)">
<input type="text" name=vcol size=5> &nbsp;
<input type="text" name=result size=4>
<br>
<input type="RESET" value="Reset" onClick="rcol = crand()">
</form>

</body>
</html>
 
Your Javascript is running before the input is created. Put your call to the crand() function at the bottom of the page, or in a window.onload.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top