By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,709 Members | 2,155 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,709 IT Pros & Developers. It's quick & easy.

Help with variables

P: 3
Help I need help with declaring a variable I am having trouble declaring a form element a variable the hta program keeps telling me that window.document.forms[0].elements[0].value is null or not an object. The same script just does not work in html

var val_1 = window.document.forms[0].elements[0].value

or with the form name and input name
example
var val_1 = window.document.form1.input1.value

same error on both

here is the script that should alert the test_value but keeps coming up undefined WHAT AM I DOING WRONG?

[HTML]<html>
<head>
<script language="javascript">
var test = window.document.forms[0].elements[0].value;
function tester()
{
alert(test)
}
</script>
</head>
<body onload="tester();">
<form name="form1">
<input name="input1" type="text" value="test_value">
<input type="button" value="try again" onclick="alert(test);">
</form>
</body>
</html>[/HTML]
Jan 6 '08 #1
Share this Question
Share on Google+
2 Replies


Markus
Expert 5K+
P: 6,050
Expand|Select|Wrap|Line Numbers
  1.  
  2. <script type="text/javascript">
  3. function tester()
  4. {
  5. test = window.document.formTest.inputTest.value;
  6. alert(test)
  7. }
  8. </script>
  9.  
That works.

The variable has to be declared inside the function - the variable must not be preceeded with 'var'; this would make the variable local and only usuable through the function (the onclick="alert(test)" wouldn't work).
Jan 6 '08 #2

P: 3
Thanks for the help Works great

Expand|Select|Wrap|Line Numbers
  1.  
  2. <script type="text/javascript">
  3. function tester()
  4. {
  5. test = window.document.formTest.inputTest.value;
  6. alert(test)
  7. }
  8. </script>
  9.  
That works.

The variable has to be declared inside the function - the variable must not be preceeded with 'var'; this would make the variable local and only usuable through the function (the onclick="alert(test)" wouldn't work).
Jan 7 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.