473,385 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

createElement / appendChild not working in IE

Atli
5,058 Expert 4TB
Hi everybody.

I'm making a Javascript system to build tables, so I can view large tables in smaller pieces. I use the "document.createElement" and "appendChild" methods to create and remove elements from my table.
Got everything working, no problems. Until I test my code in IE.

After a little investigation, I create this:
Expand|Select|Wrap|Line Numbers
  1. <div id="ListContainer" class="Info">
  2. <table id="ListTable">
  3.     <tr>
  4.         <td>Hello</td>
  5.     </tr>
  6. </table>
  7. </div>
  8.  
  9. <script type="text/javascript">
  10.     var table = document.getElementById("ListTable");
  11.     var newRow = document.createElement("tr");
  12.     var newCol = document.createElement("td");
  13.     newCol.innerHTML = "Row2";
  14.     newRow.appendChild(newCol);
  15.     table.appendChild(newRow);
  16. </script>
  17.  
As before, this works in Firefox, which I use to debug my script. But fails in IE.
Weird thing is that IE usually flashes the error icon in the bottom-left corner when a script is broken, but that doesn't happen now.
Nothing happens, no errors, no changes.

I can't find anything wrong with this code.
Anybody know what is going on there?
Jan 21 '08 #1
13 16150
gits
5,390 Expert Mod 4TB
hi ...

i think i remember that in IE you have to use the tablebody (<tbody>) to append rows ... that will work in FF/Moz too ... so you don't need a different code ... just extend it ... :)

kind regards
Jan 21 '08 #2
Atli
5,058 Expert 4TB
Ahhh, yet another brilliant IE feature!

Just added a <tbody> element between my table and the new rows and everything started working perfectly.

Thanks mate :)
Jan 22 '08 #3
gits
5,390 Expert Mod 4TB
hi Atli ...

in this case i think IE handles the table quite strict ... while the other browsers do not ... have a look here ...

kind regards
Jan 22 '08 #4
Atli
5,058 Expert 4TB
That's interesting.

I wonder tho. If you take a look further down the page, you can see that the examples there don't use the <theader>, <tfooter> and <tbody> elements.
Wonder if they should be optional rather than required as IE handles them.

Lots of other interesting stuff there to.
Thanks mate.
Jan 22 '08 #5
gits
5,390 Expert Mod 4TB
as far as i understood ... browsers should be able to handle it gracefully for backwards-compatibility so IE's strict handling is really strange behaviour ... especially since it handles other things not the strict way ... the good news are: the other browsers can handle it too so we don't need to code twice ;) ... or need some workarounds this time ... while discovering it, it seems to be silly but once you know then you may handle it very straight :) ...

kind regards
Jan 22 '08 #6
acoder
16,027 Expert Mod 8TB
Yes, IE's strict handling in this case is indeed strange. I think, though, that it is problematic simply because of the number of hours spent figuring out what the problem might be as well as the sore foreheads, damaged keyboards and grey hairs!
Jan 23 '08 #7
gits
5,390 Expert Mod 4TB
Yes, IE's strict handling in this case is indeed strange. I think, though, that it is problematic simply because of the number of hours spent figuring out what the problem might be as well as the sore foreheads, damaged keyboards and grey hairs!
yes ... i agree ... or simply ask here in the forum to avoid that :)
Jan 23 '08 #8
acoder
16,027 Expert Mod 8TB
I've documented this and other quirky or outright buggy behaviour in Browser Bugs, Quirks and Inconsistencies.
Jan 23 '08 #9
mumf83
1
Hello,

I've been having problems with appendChild on IE. Everything works on other browsers. I found this thread and amended my script to what has been explained above, by adding a <tbody> but it still doesn't work in IE. the code is below. can anyone spot anything I'm doing wrong..?

My Ajax code:
Expand|Select|Wrap|Line Numbers
  1.         for(var i=0; i<_post.length; i++)
  2.         {
  3.             rank++;
  4.             var _time = response.getElementsByTagName('time')[i].firstChild.data;
  5.             var _name = response.getElementsByTagName('name')[i].firstChild.data
  6.  
  7.             var tb= document.getElementById('posts');                
  8.             var tr = document.createElement('TR');    
  9.             tb.appendChild(tr);
  10.             var td = document.createElement('TD');
  11.             tr.appendChild(td);
  12.             td.innerHTML= rank;
  13.             var td2 = document.createElement('TD');
  14.             tr.appendChild(td2);
  15.             td2.innerHTML= _name;
  16.             var td3 = document.createElement('TD');
  17.             tr.appendChild(td3);
  18.             td3.innerHTML= _time;
  19.         }
  20.  
and my HTML code:

Expand|Select|Wrap|Line Numbers
  1.     <TABLE style='float:left' border='1' id='rankingTable'>
  2.     <TR><TH COLSPAN='3'>Top 100</TH></TR>
  3.     <TR><TH>#</TH><TH>Name</TH><TH>Score</TH></TR>
  4.  
  5.     <tbody id='posts'>
  6.     </tbody>
  7.  
  8.     </TABLE>
  9.  
Hope to here from someone soon?
Apr 17 '08 #10
acoder
16,027 Expert Mod 8TB
First of all, welcome to Bytes!

Now, onto your question: whenever you use createElement and appendChild, start off by creating the elements in parent to child order and then append them in reverse order, i.e. from child to parent. For example, your code becomes:
Expand|Select|Wrap|Line Numbers
  1. var tb= document.getElementById('posts');                
  2. var tr = document.createElement('TR');    
  3. var td = document.createElement('TD');
  4. td.innerHTML= rank;
  5. var td2 = document.createElement('TD');
  6. td2.innerHTML= _name;
  7. var td3 = document.createElement('TD');
  8. td3.innerHTML= _time;
  9. tr.appendChild(td);
  10. tr.appendChild(td2);
  11. tr.appendChild(td3);
  12. tb.appendChild(tr);
  13.  
Apr 17 '08 #11
tables have there own dom functions
Expand|Select|Wrap|Line Numbers
  1. row = table.insertRow()
  2. cell = row.insertCell()
both functions take params for where the index should be
functions work in both IE and FF
Apr 22 '08 #12
gits
5,390 Expert Mod 4TB
yes that is right ... for a comparison of compat-issues with different dom-operations you may have a look here ...

kind regards
Apr 22 '08 #13
@gits, thanks for your suggestion , it helped me in solving the same problem
Mar 14 '12 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

25
by: kie | last post by:
hello, i have a table that creates and deletes rows dynamically using createElement, appendChild, removeChild. when i have added the required amount of rows and input my data, i would like to...
2
by: kie | last post by:
hello, when i create elements and want to assign events to them, i have realised that if the function assigned to that element has no parameters, then the parent node values can be attained. ...
2
by: Ivo | last post by:
I have this code to add a row to a table: var r=document.createElement('tr'); var c=document.createElement('td'); var cn=c.cloneNode(false); cn.className='tdname';...
4
by: sg_maat | last post by:
I have a little problem with createElement(and msie). I was experimenting a bit with createElement and made the following script: <script> var tmp = document.createElement("div");...
3
by: Arpan | last post by:
Using DOM, this is how I am appending data to an existing XML file which is named AppendData.xml (the root element of AppendData.xml is <ProductList>): <script runat="server"> Sub...
9
by: Andrew Poulos | last post by:
When I run the following code in FF 1.5 and 2, and IE 6 on my computer the table and image appears. When I run it on the client's XP machine it appears under FF 1.5 but under IE 6 the window is...
3
by: acecraig100 | last post by:
I am fairly new to Javascript. I have a form that users fill out to enter an animal to exhibit at a fair. Because we have no way of knowing, how many animals a user may enter, I created a table...
7
by: Tarik Monem | last post by:
Why am I having so much trouble with using DOM in IE & Opera to create, then remove an Object & Embedded element? Here's the code that works in Firefox (Mac/Win/Linux) and Safari, but not on IE or...
23
by: vunet | last post by:
It is recommended by some sources I found to create IFrames in IE using document.createElement('<iframe src="#">') instead of document.createElement('iframe'). Why and what browser versions to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.