M
Mark Hannon
I am trying to initialize an array only once so it can be seen & used by
any functions that need it. As I understand it, if a variable is
declared by itself outside of any functions, its scope is global and any
functions should be able to access it.
I have been having trouble getting this to work. In the 1st of the 2
examples below I initialize the Array "initArray" with 4 form text
fields. When I try to use initArray[] inside either of the 2 functions,
nothing happens. In the 2nd example, I initialize 2 different versions
of the same array from inside the 2 functions and the code works fine.
While the 2nd example works, it seems like a clumsy way to work.
How would I declare & initialize an array only once so I can use it in
any functions that need it?
Thanks,
Mark
*****************Example 1 - Doesn't work*******************
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<title>Initialize array</title>
<meta http-equiv="Content-type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
var initArray = [document.testForm.field1, document.testForm.field2,
document.testForm.field3, document.testForm.field4];
function getValuesA(){
var valuesOne = ["One", "Two", "Three", "Four"];
for(var i = 0; i <= 4; i++ ){
initArray.value = valuesOne;
}
}
function getValuesB(){
var valuesTwo = ["Five", "Six", "Seven", "Eight"];
for(var i = 0; i <= 4; i++ ){
initArray.value = valuesTwo;
}
}
</script>
</head>
<body>
<form name="testForm" action="" method="POST" enctype="text/plain">
<input name="field1" type="text" value="" size="5" maxlength="5"><br>
<input name="field2" type="text" value="" size="5" maxlength="5"><br>
<input name="field3" type="text" value="" size="5" maxlength="5"><br>
<input name="field4" type="text" value="" size="5" maxlength="5"><br>
<input name="myButton1" type="button" value="Test1"
onClick="getValuesA()">
<input name="myButton2" type="button" value="Test2"
onClick="getValuesB()">
</form>
</body>
</html>
*********************Example 2 - Works!*********************
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<title>Initialize array 2</title>
<meta http-equiv="Content-type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function getValuesA(){
var initArrayA = [document.testForm.field1, document.testForm.field2,
document.testForm.field3, document.testForm.field4];
var valuesOne = ["One", "Two", "Three", "Four"];
for(var i = 0; i <= 4; i++ ){
initArrayA.value = valuesOne;
}
}
function getValuesB(){
var initArrayB = [document.testForm.field1, document.testForm.field2,
document.testForm.field3, document.testForm.field4];
var valuesTwo = ["Five", "Six", "Seven", "Eight"];
for(var i = 0; i <= 4; i++ ){
initArrayB.value = valuesTwo;
}
}
</script>
</head>
<body>
<form name="testForm" action="" method="POST" enctype="text/plain">
<input name="field1" type="text" value="" size="5" maxlength="5"><br>
<input name="field2" type="text" value="" size="5" maxlength="5"><br>
<input name="field3" type="text" value="" size="5" maxlength="5"><br>
<input name="field4" type="text" value="" size="5" maxlength="5"><br>
<input name="myButton1" type="button" value="Test1"
onClick="getValuesA()">
<input name="myButton2" type="button" value="Test2"
onClick="getValuesB()">
</form>
</body>
</html>
any functions that need it. As I understand it, if a variable is
declared by itself outside of any functions, its scope is global and any
functions should be able to access it.
I have been having trouble getting this to work. In the 1st of the 2
examples below I initialize the Array "initArray" with 4 form text
fields. When I try to use initArray[] inside either of the 2 functions,
nothing happens. In the 2nd example, I initialize 2 different versions
of the same array from inside the 2 functions and the code works fine.
While the 2nd example works, it seems like a clumsy way to work.
How would I declare & initialize an array only once so I can use it in
any functions that need it?
Thanks,
Mark
*****************Example 1 - Doesn't work*******************
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<title>Initialize array</title>
<meta http-equiv="Content-type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
var initArray = [document.testForm.field1, document.testForm.field2,
document.testForm.field3, document.testForm.field4];
function getValuesA(){
var valuesOne = ["One", "Two", "Three", "Four"];
for(var i = 0; i <= 4; i++ ){
initArray.value = valuesOne;
}
}
function getValuesB(){
var valuesTwo = ["Five", "Six", "Seven", "Eight"];
for(var i = 0; i <= 4; i++ ){
initArray.value = valuesTwo;
}
}
</script>
</head>
<body>
<form name="testForm" action="" method="POST" enctype="text/plain">
<input name="field1" type="text" value="" size="5" maxlength="5"><br>
<input name="field2" type="text" value="" size="5" maxlength="5"><br>
<input name="field3" type="text" value="" size="5" maxlength="5"><br>
<input name="field4" type="text" value="" size="5" maxlength="5"><br>
<input name="myButton1" type="button" value="Test1"
onClick="getValuesA()">
<input name="myButton2" type="button" value="Test2"
onClick="getValuesB()">
</form>
</body>
</html>
*********************Example 2 - Works!*********************
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<title>Initialize array 2</title>
<meta http-equiv="Content-type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function getValuesA(){
var initArrayA = [document.testForm.field1, document.testForm.field2,
document.testForm.field3, document.testForm.field4];
var valuesOne = ["One", "Two", "Three", "Four"];
for(var i = 0; i <= 4; i++ ){
initArrayA.value = valuesOne;
}
}
function getValuesB(){
var initArrayB = [document.testForm.field1, document.testForm.field2,
document.testForm.field3, document.testForm.field4];
var valuesTwo = ["Five", "Six", "Seven", "Eight"];
for(var i = 0; i <= 4; i++ ){
initArrayB.value = valuesTwo;
}
}
</script>
</head>
<body>
<form name="testForm" action="" method="POST" enctype="text/plain">
<input name="field1" type="text" value="" size="5" maxlength="5"><br>
<input name="field2" type="text" value="" size="5" maxlength="5"><br>
<input name="field3" type="text" value="" size="5" maxlength="5"><br>
<input name="field4" type="text" value="" size="5" maxlength="5"><br>
<input name="myButton1" type="button" value="Test1"
onClick="getValuesA()">
<input name="myButton2" type="button" value="Test2"
onClick="getValuesB()">
</form>
</body>
</html>