Link to home
Start Free TrialLog in
Avatar of chsalvia
chsalvia

asked on

Creating tables in Internet Explorer

I wrote the following code as a learning experiment to create a simple table using createElement and insertBefore.  The code simply creates a button which then lets you add more tables to the page.  The table appears as a simple blue box.

But I want each new table to appear next to the previous table, so if you keep clicking the create table button, new tables appear left to right.  This is how the code behaves in Firefox.  But in Internet Explorer, the tables appear vertically, as if it does not understand the "float" property.

Why does this happen in Internet Explorer, and what are some fixes?


<html>
<head>
<style>
.adboxdiv {
      float: left;
      margin: 0px 15px 15px 0px
}
</style>

<script language = "Javascript">

function addelement() {
      var pdiv = document.createElement("div");
      var ptable = document.createElement("table");
      var ptbody = document.createElement("tbody");
      var prow = document.createElement("tr");
      var pcell = document.createElement("td");

      pdiv.setAttribute("class","adboxdiv");
      ptable.setAttribute("border","0");
      pcell.setAttribute("width","100");
      pcell.setAttribute("height","50");
      pcell.setAttribute("bgColor","#0000FF");

      prow.appendChild(pcell);
      ptbody.appendChild(prow);
      ptable.appendChild(ptbody);
      pdiv.appendChild(ptable);

      document.getElementById("maincell").insertBefore(pdiv, document.getElementById('tablediv'));            
}

</script>

</head>
<body>
<table id = "maintable" border = "0" cellspacing = "0" cellpadding = "0" width = "100%">
<tr><td id = "maincell">

<div id = "tablediv" class = "adboxdiv"><table border = "0"><tr><td width = "100" height = "50" bgColor = =#FF0000"></td></tr></table></div>

</td></tr>

</table>

<input type = "submit" value = "add a table" onclick = "addelement()">
<input type = "submit" value = "delete table" onclick = "removeelement()">

</body>
</html>
ASKER CERTIFIED SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am not sure about the technical reasons why msIE behaves this way, but I know msIE has difficulties which some of the basic dom operations like setAttribute.

-r-
Avatar of chsalvia
chsalvia

ASKER

Thanks!  That worked nicely.