Re: Creating a table in javascript



fjleon@xxxxxxxxx wrote:

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?

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);
}
.