473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Help with variables

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
2 1047
Markus
6,050 Expert 4TB
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
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

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

Similar topics

3
by: Robert | last post by:
Hello, Can anyone help with this? On my online order form, I need to send a few variables to my credit-card processor. These variables are for non-secure customer comments, and will be sent...
1
by: Dave Smithz | last post by:
Hi there, I have implemented a login sequence using session variables for a PHP/MySQL DB which I took over development. I read some books and this NG and it seemed straight forward. However the...
15
by: Alfonso Morra | last post by:
Hi, I have some code from an example, that I want to retrofit into my project. The code from the example has the following line: SharedAppenderPtr myAppender( new...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
9
by: hope | last post by:
Hi Access 97 I'm lost on this code please can you help ================================= Below is some simple code that will concatenate a single field's value from multiple records into a...
11
by: dhnriverside | last post by:
Hi peeps Ok, so I thought I'd have a go at making a console app in VS2k5... I haven't written any windows apps for years, let alone dos apps (been web programming) and I've hit a dumb error... ...
4
by: DOTNET | last post by:
Hi, Anybody help me regarding this error: I am assigning the values to the session variables when the button is clicked and passing these session variables to the next page and when I am...
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
6
by: karafire2003 | last post by:
Hey there i was wondering if someone can help me with my program. I thought i had it down...but i'm having some problems. Here's the assignment. Write a C function named change() that accepts a...
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.