...

View Full Version : array and 'is null or not an object'



Lau
02-06-2004, 12:09 AM
I was wondering why I'm getting this error message.
here's what my code look like:
in my HTML body:


<form name="EmpInfo">
<table width="700" frame=box rules=none border=1 name="hours_input" align="left">
<tr><td colspan=7><I>Input the number of hours the employee has worked this week:</I></td></tr>
<tr><th align=left width="15%">Monday</th><th align=left width="15%">Tuesday</th><th align=left width="15%">Wednesday</th>
<th align=left width="15%">Thrusday</th><th align=left width="15%">Friday</th>
<th align=left width="15%">Saturday</th><th align=left width="15%">Sunday</th></tr>
<tr><td><input type="text" name="day[0]" value=0 size=2></td>
<td><input type="text" name="day[1]" value=0 size=2></td>
<td><input type="text" name="day[2]" value=0 size=2></td>
<td><input type="text" name="day[3]" value=0 size=2></td>
<td><input type="text" name="day[4]" value=0 size=2></td>
<td><input type="text" name="day[5]" value=0 size=2></td>
<td><input type="text" name="day[6]" value=0 size=2></td></tr>
</table>
<input type="button" name="submit_hours" value="Get Salary" onClick="getSalary(this.form);">
</form>


in my Javascript part:


function getSalary(myForm) {
HoursCheck();

}

function HoursCheck() {
var h = 0;
var dayName = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];

for (var i=0; i<7; i++) {
h = document.EmpInfo.day[i].value;
if (h < 0 || h >15)
alert('You could not have worked '+ h +' hours on' + dayName[i]);
}
}

when I click on the get Salary button, I get a "'document.EmpInfo.day' is null or not an object" error.
I don't get what the problem is

Willy Duitt
02-06-2004, 12:49 AM
Try this:
<script type="text/javascript">
function getSalary(myForm) {
HoursCheck();

}

function HoursCheck() {
var h = 0;
var dayName = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];

for (var i=0; i<7; i++) {
h = document.EmpInfo['day'+i].value;
if (h < 0 || h >15)
alert('You could not have worked '+ h +' hours on' + dayName[i]);
}
}

</script>
</HEAD>

<BODY>
<form name="EmpInfo">
<table width="700" frame=box rules=none border=1 name="hours_input" align="left">
<tr><td colspan=7><I>Input the number of hours the employee has worked this week:</I></td></tr>
<tr><th align=left width="15%">Monday</th><th align=left width="15%">Tuesday</th><th align=left width="15%">Wednesday</th>
<th align=left width="15%">Thrusday</th><th align=left width="15%">Friday</th>
<th align=left width="15%">Saturday</th><th align=left width="15%">Sunday</th></tr>
<tr><td><input type="text" name="day0" value=0 size=2></td>
<td><input type="text" name="day1" value=0 size=2></td>
<td><input type="text" name="day2" value=0 size=2></td>
<td><input type="text" name="day3" value=0 size=2></td>
<td><input type="text" name="day4" value=0 size=2></td>
<td><input type="text" name="day5" value=0 size=2></td>
<td><input type="text" name="day6" value=0 size=2></td></tr>
</table>
<input type="button" name="submit_hours" value="Get Salary" onClick="getSalary(this.form);">
</form>

.....Willy

Lau
02-06-2004, 12:53 AM
Nice Willy... Thanks!
Could you explain why I can't use an array here??

Willy Duitt
02-06-2004, 01:26 AM
Originally posted by Lau
Nice Willy... Thanks!
Could you explain why I can't use an array here??

You could, but you did not make an array, assign any values nor
properly assigned the values to your input name. Besides, all that
would seem like way too much work.

However, here is one way to use an array of the forms.elements
and do the same thing without input names but it is unadvised
since it would make it more difficult to pass the values later on.

<script type="text/javascript">
function getSalary(myForm) {
HoursCheck();
}

function HoursCheck() {
var dayName = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
for (var i=0; i<document.forms[0].elements.length; i++) {
h = document.forms[0].elements[i];
if (h.type=='text' && h.value < 0 || h.value >15)
alert('You could not have worked '+ h.value +' hours on ' + dayName[i]);
}
}

</script>
</HEAD>

<BODY>
<form name="EmpInfo">
<table width="700" frame=box rules=none border=1 name="hours_input" align="left">
<tr><td colspan=7><I>Input the number of hours the employee has worked this week:</I></td></tr>
<tr><th align=left width="15%">Monday</th><th align=left width="15%">Tuesday</th><th align=left width="15%">Wednesday</th>
<th align=left width="15%">Thrusday</th><th align=left width="15%">Friday</th>
<th align=left width="15%">Saturday</th><th align=left width="15%">Sunday</th></tr>
<tr><td><input type="text" name="day0" value=0 size=2></td>
<td><input type="text" value=0 size=2></td>
<td><input type="text" value=0 size=2></td>
<td><input type="text" value=0 size=2></td>
<td><input type="text" value=0 size=2></td>
<td><input type="text" value=0 size=2></td>
<td><input type="text" value=0 size=2></td></tr>
</table>
<input type="button" name="submit_hours" value="Get Salary" onClick="getSalary(this.form);">
</form>

.....Willy



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum