www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 07-28-2006, 11:36 AM
    SaipanMan95 SaipanMan95 is offline
    Registered User
     
    Join Date: Jul 2006
    Posts: 13
    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
    At first I just ignored this error and posted the code to server but the desired div elements and data wouldn't display. On my own pc it works fine, so I am assumming its this one error message.

    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>
    any ideas?
    Reply With Quote
      #2  
    Old 07-28-2006, 12:51 PM
    gil davis's Avatar
    gil davis gil davis is offline
    Nobody important
     
    Join Date: Nov 2002
    Posts: 4,456
    Not sure, but...
    Code:
    for( var i = 1; i <=(noOfDaysInmnth+(j-1)); i++){
    ...
      j++;
    I think the end of the for loop keeps moving.

    Put this at the end of the function writeDivs():
    Code:
    if (!document.getElementById(id)) {alert("bad ID = " + id);}
    If the value of id > "d42" then you will know that the for loop went too far.
    Reply With Quote
      #3  
    Old 07-28-2006, 01:21 PM
    SaipanMan95 SaipanMan95 is offline
    Registered User
     
    Join Date: Jul 2006
    Posts: 13
    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;
    after the writeDivs(...) function and no more errors...

    Thanks!!!
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 04:32 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.