|
|||||||
| JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...) |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
getElementById is null or not an object
Hi All,
I have been dealing with this terrible little error now for a couple of days. I am sure it is something basic. Here is the situation: I have set aside 42 Div statements all with a ref Id incrementing from d1 to d42 and reference them via a for loop and inside that loop I pass all the values to a simple function that attempts to write to the div id="d1" and so on. Amazingly it runs fine all the data in appears where it shoul; however, I get this annoying error message saying that document.getElementById(...) is null or not an object. Here is some sample code: HTML Code:
<!-- The function to reset the date values in the table cells.--> // function require currentdate, current day, and number of days in month function fillDates(cDate, cDay, noOfDaysInmnth) { var startDay = (((cDate - (cDay +1))%7)-7); var slotInd, slotName, val, events; var j = (startDay)*(-1)+1; var m = cmnth + 1; //alert(getEvents(7,15)); for( var i = 1; i <=(noOfDaysInmnth+(j-1)); i++){ slotName = "d"+j; val = i; if( val < 1 || val > noOfDaysInmnth){val = " ";} else{val = i;} events = "<br/><i><small>" + getEvents(m,i) + "</small></i>"; writeDivs(val, slotName, events);//Writes to Div tags via innerHtml j++; }//End For Loop }//fillDates() <!--"writes the date information to the div blocks" --> function writeDivs(d, id, events){ //d = val; id = slotName; events = events; var x, evt, d2, _id; if(events == null){ evt = 0;} if(events != null){ evt = events;} if(d == null){ d2 = 0;} if(d != null){ d2 = d;} if(d == 0){ evt = " "; } x = document.getElementById(id).innerHTML = d2 + evt;//<-this is were the error is occurring }//end writit here is the table and div structure I use: HTML Code:
<table border=".5" cellpadding="0" cellspacing="" width="100%" style="border-color:#003DAE"> <tr style="border-color:#003DAE"><th>Sun</th><th>Mon</th><th>Tue</th> <th>Wed</th><th>Thu</th><th>Fri</th> <th>Sat</th></tr> <tr> <td><div class="div_1" id="d1">.</div></td> <td><div class="div_2" id="d2">.</div></td> <td><div class="div_3" id="d3">.</div></td> <td><div class="div_4" id="d4">.</div></td> <td><div class="div_5" id="d5">.</div></td> <td><div class="div_6" id="d6">.</div></td> <td><div class="div_7" id="d7">.</div></td> </tr> <tr> <td><div class="div_8" id="d8">.</div></td> <td><div class="div_9" id="d9">.</div></td> <td><div class="div_10" id="d10">.</div></td> <td><div class="div_11" id="d11">.</div></td> <td><div class="div_12" id="d12">.</div></td> <td><div class="div_13" id="d13">.</div></td> <td><div class="div_14" id="d14">.</div></td> </tr> <tr> <td><div class="div_15" id="d15">.</div></td> <td><div class="div_16" id="d16">.</div></td> <td><div class="div_17" id="d17">.</div></td> <td><div class="div_18" id="d18">.</div></td> <td><div class="div_19" id="d19">.</div></td> <td><div class="div_20" id="d20">.</div></td> <td><div class="div_21" id="d21">.</div></td> </tr> <tr> <td><div class="div_22" id="d22">.</div></td> <td><div class="div_23" id="d23">.</div></td> <td><div class="div_24" id="d24">.</div></td> <td><div class="div_25" id="d25">.</div></td> <td><div class="div_26" id="d26">.</div></td> <td><div class="div_27" id="d27">.</div></td> <td><div class="div_28" id="d28">.</div></td> </tr> <tr> <td><div class="div_29" id="d29">.</div></td> <td><div class="div_30" id="d30">.</div></td> <td><div class="div_31" id="d31">.</div></td> <td><div class="div_32" id="d32">.</div></td> <td><div class="div_33" id="d33">.</div></td> <td><div class="div_34" id="d34">.</div></td> <td><div class="div_35" id="d35">.</div></td> </tr> <tr> <td><div class="div_36" id="d36">.</div></td> <td><div class="div_37" id="d37">.</div></td> <td><div class="div_38" id="d38">.</div></td> <td><div class="div_39" id="d39">.</div></td> <td><div class="div_40" id="d40">.</div></td> <td><div class="div_41" id="d41">.</div></td> <td><div class="div_42" id="d42">.</div></td> </tr> </table> |
|
#2
|
||||
|
||||
|
Not sure, but...
Code:
for( var i = 1; i <=(noOfDaysInmnth+(j-1)); i++){
...
j++;
Put this at the end of the function writeDivs(): Code:
if (!document.getElementById(id)) {alert("bad ID = " + id);}
|
|
#3
|
|||
|
|||
|
Thanks....
I tried that alert statement and sure enough the id value was incrementing to 43 and stopping... I went a head and added... HTML Code:
if(j > 0 && j < 42) j++; else break; Thanks!!! |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|