Re: Creating a table in javascript



fjleon@xxxxxxxxx wrote:
Ok, apparently IE needs that rows go inside a tbody, otherwise it
doesn't work.
So i added the tbody and it works.

You may find it easier to use inesertRow rather than add a tbody. It requires a lot less code, e.g.:

var newTable = document.createElement('table');
var newRow = newTable.insertRow(-1);
var newCell = newRow.insertCell(-1);

The table now has a single row and cell and is ready to be added to the document - no need to explicitly create a tbody, nor to create then add elements.

<URL:http://developer.mozilla.org/en/docs/DOM:table#Methods>


Now, i need to add a <select> with 2 options inside it.

I tried doing it this way:

var td3 = document.createElement("TD");
var selectsexo = document.createElement("select");
var masculino=new Option("MASCULINO","M");
var femenino=new Option("FEMENINO","F");
selectsexo.appendChild(masculino);
selectsexo.appendChild(femenino);
td3.appendChild(selectsexo);

But again, doesn't work on IE.

What will work in IE is (wrapped for posting):

var selectsexo = document.createElement("select");
selectsexo.options[selectsexo.options.length] =
new Option("MASCULINO","M");
selectsexo.options[selectsexo.options.length] =
new Option("FEMENINO","F");
td3.appendChild(selectsexo);

There is a relevant thread here:

<URL:http://groups.google.com.au/group/comp.lang.javascript/browse_frm/thread/226caadb3bd3ca60/61dc315bf5d3baea?q=new+option+text+value+robg&rnum=1#61dc315bf5d3baea>



--
Rob
.



Relevant Pages

  • Re: Adding rows to table inside a form for input
    ...     function addRow{ ... simply want the rows in your tbody so doing ... var tbody = document.getElementById.getElementsByTagName ... input1.value = 'food item'; ...
    (comp.lang.javascript)
  • Re: Urgent:Linkbutton problem!
    ... S. Justin Gengo, MCP ... <TBODY> ... var Page_ValidationActive = false; ... function ValidatorOnSubmit() { ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: RemoveChild and IE crash !
    ... var element = document.getElementById; ... I am calling this function many times in my DHTMl page. ... Simple remove the TBODY if you want to remove everything in it. ... I would like to see a sample page that shows that innerHTML will beat ...
    (microsoft.public.scripting.jscript)
  • Re: another wacky IE problem
    ... Is table an HTML table element? ... section element (thead, tfoot or tbody). ... one will be added by the browser when it loads the ... var table = document.getElementById; ...
    (comp.lang.javascript)
  • Ynet HTML Code Yaron BC and L.Adviser Letter
    ... content="Ynet, Yedioth Ahronoth Group's web site, is Israel's most ... <SCRIPT language=JavaScript> ... <TBODY> ... var rurl = ...
    (soc.culture.israel)