SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Dec 2006
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    document.getElementById error

    Hi all,

    I am getting a strange error while trying to get-ElementById()- a tbody element. Here is the chunk of code

    Code:
    function displayStats(data, period, routesNo) 
    {
    	var tb = document.getElementById("resultsTbody");
    	var inforow = document.createElement('tr');
    	inforow.setAttribute("id", "aRow");
    		
    	var infocell = document.createElement('td');
    	infocell.setAttribute("id", "infoCell");
    	infocell.innerHTML = "string";
    	
    	inforow.appendChild(infocell);
    	tb.appendChild(inforow);
               ....
    }
    ......
    ......
    HTML Code:
    <table id="statisticsResultsTbl" border="1" width="600" align="center">
    	<thead class="tbodies" id="headTbody">
    		<tr class="darkBlue">
    			<td id="statsMainHd" align="center" class="db_title_12pt">Statistics Results</td>
    		</tr>
    	</thead>
    	<tbody id="resultsTbody"></tbody>
    </table>
    In FF i get the error: "tb has no properties"
    in IE7: 'null' is null or not an object
    in opera is not working at all

    What the heck...i have the same code elsewhere and it's working...

    any ideas??

    thanks in advance

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When and where do you call the function?
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    SitePoint Member
    Join Date
    Dec 2006
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the above are all in the same document (html).

    The function displayStats() is in the <head/>

    in the body i am calling the function as follows, before any other HTML code:

    Code:
    if($json) {
         <script type="text/JavaScript" language="JavaScript">
    	displayStats($json_res, $str1, $str2);
         </script>
    }
    The above is an example...the program is not in php but in java...however is a custom way of passing/parsing variables to html so it won't help you if i paste the exact thing here...

    if i put an alert(data); in the first line of the displayStats() func or a dojo.debug(data); i get the json objects correctly.

    The error in both browsers points to the "tb.appendChild(inforow)" line.

  4. #4
    SitePoint Member
    Join Date
    Dec 2006
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have two tables on that page...I want the first to remain there and append the new table after the first (lets say i am now creating the whole table in JS)

    dojo.dom.insertAfter(node, ref, force) can do that, however, the ref is failing and this is because i can getElementById only the <body> element...
    All the rest elements in the body it's like they don't exist in the page's dom, and i get the very same error, allthough i can see them in firebug

  5. #5
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It sounds like you aren't waiting for the DOM to finish being built.

    Try calling the function via an onload handler. Also, the language attribute is deprecated, so you shouldn't use it.
    Code:
         <script type="text/JavaScript">
    	window.onload = function() {
    		displayStats($json_res, $str1, $str2);
    	}
         </script>
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  6. #6
    SitePoint Member
    Join Date
    Dec 2006
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ohhh...YES! Even before i try it, i believe you are absolutely correct.
    It get's in the function before those elements get created in the dom! Thanks for waking me up...

  7. #7
    SitePoint Member
    Join Date
    Dec 2006
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After trying it: That was it of course. Thanks again!


Bookmarks

Posting Permissions

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