Ok, apparently IE needs that rows go inside a tbody, otherwise it
doesn't work.
So i added the tbody and it works.
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.
So i am now doing it like this:
var masculino = document.createElement("option");
masculino.setAttribute("value","M");
masculino.text="Masculino";
masculino.innerText="Masculino";
var femenino = document.createElement("option");
femenino.setAttribute("value","F");
femenino.text="Femenino";
femenino.innerText="Femenino";
Mozilla doesn't support innerText, and IE doesn't support text, so i am
using both.
Is there a better way?
Duncan Booth - 30 May 2006 16:17 GMT
> var masculino = document.createElement("option");
> masculino.setAttribute("value","M");
[quoted text clipped - 8 lines]
> using both.
> Is there a better way?
I don't know what you tried, but IE supports the 'text' attribute just
fine. This should work equally well on either browser:
var selectsexo = document.createElement("select");
var masculino = document.createElement("option");
masculino.value = "M";
masculino.text="Masculino";
var femenino = document.createElement("option");
femenino.value = "F";
femenino.text="Femenino";
selectsexo.options.add(masculino);
selectsexo.options.add(fememino);
Or reduce the repetition by using a loop:
var selectsexo = document.createElement("select");
var options = [["M", "Masculino"], ["F", "Femenino"]];
for (var i=0; i < options.length; i++) {
var opt = document.createElement("option");
opt.value = options[i][0];
opt.text = options[i][1];
selectsexo.options.add(opt);
}
fjleon@gmail.com - 30 May 2006 19:44 GMT
these 2 lines give errors:
selectsexo.options.add(masculino);
selectsexo.options.add(fememino);
http://www.w3schools.com/htmldom/dom_obj_select.asp lists an "add"
function, but doesn't give examples.
Marc - 30 May 2006 20:47 GMT
> these 2 lines give errors:
>
[quoted text clipped - 3 lines]
> http://www.w3schools.com/htmldom/dom_obj_select.asp lists an "add"
> function, but doesn't give examples.
function addOptions(object, oValue, oText) {
var defaultSelected = true; var selected = true;
var optionName = new Option(oText, oValue, defaultSelected, selected)
var length = object.length;
object.options[length] = optionName;
}
object = document.getElementById("yourselect")
RobG - 31 May 2006 10:11 GMT
> 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.
>
[quoted text clipped - 9 lines]
>
> 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/226caad
b3bd3ca60/61dc315bf5d3baea?q=new+option+text+value+robg&rnum=1#61dc315bf5d3baea>

Signature
Rob