Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Lau
    Lau is offline
    New Coder
    Join Date
    Jan 2004
    Location
    Malden, MA
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    array and 'is null or not an object'

    I was wondering why I'm getting this error message.
    here's what my code look like:
    in my HTML body:
    Code:
    <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:
    Code:
      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

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    Lau
    Lau is offline
    New Coder
    Join Date
    Jan 2004
    Location
    Malden, MA
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nice Willy... Thanks!
    Could you explain why I can't use an array here??

  • #4
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •