473,387 Members | 1,757 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,387 software developers and data experts.

Creating a table in javascript

Hi, i want to create a table declared inside a <td> , which already
exists as follows:

<tr>
<td id="prueba" colspan="4">
</td>
</tr>

I have a button somewhere calling this script, which i have reduced as
much for this post:

function prueba()
{
var tdpadre = document.getElementById("prueba");
var tabla = document.createElement("table");
tabla.setAttribute("id","tablaaseg");
tabla.setAttribute("name","tablaaseg");
tabla.setAttribute("width","100%");
var cantidadasegurados =
document.getElementById("txtnumbene").value;
for (i=0;i<cantidadasegurados;i++)
{
var row = document.createElement("tr");
var td1 = document.createElement("TD");
var inputnombre = document.createElement("input");
td1.appendChild(inputnombre);
row.appendChild(td1);
tabla.appendChild(row);
}
tdpadre.appendChild(tabla);
}

Documentation:
-Get the td where i am going to insert the table
-Create the table and set some attributes
-Get the value of an input, which contains a number. I will be creating
x numbers of rows, with 1 td inside each row
-The td will have an input inside it, so i create it.
-Append the td as a child of the tr, then the tr as a child of the
table
-Finally, add the table as child of the parent td.

This works on mozilla and opera, but not on IE. I dont get a yellow
icon , it just doesn't do anything.

Can anyone tell me what i am doing wrong?

May 30 '06 #1
5 12306
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?

May 30 '06 #2
fj****@gmail.com 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);
}
May 30 '06 #3
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.

May 30 '06 #4

<fj****@gmail.com> schreef in bericht
news:11**********************@g10g2000cwb.googlegr oups.com...
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.


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")
May 30 '06 #5
fj****@gmail.com 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
May 31 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David Bradbury | last post by:
I currently have an iframe on a webpage into which users can insert content. They can further customise the text as I've included buttons such as Bold, Italic, Bullet point etc. This is done along...
12
by: enak | last post by:
I have found some code that shows how to convert an html form to Word. The message said to simply put the following in the Page_load: Response.ContentType = "application/ms-word"...
4
by: GRenard | last post by:
Hi, I'm trying just to display a table on a webpage using DOM elements created dynamically. I really don't understand why IE doesn't display the document successfully... If I make a...
1
by: Rako | last post by:
My problem is: I want to create an index to any of the available picture-groups. This index is a table of thumbs with a scrollbar. If you click on the thumb, you get the full picture displayed. ...
1
by: virajitha | last post by:
Hi .... I am facing a major problem in writing a javascript that will load an xml document dynamically and posts it to the next page when clicked on a hyperlink. The problem is that in the...
5
eragon
by: eragon | last post by:
I wrote this function to create a new file when the user posts in my forums, and its not creating a new file, can you help me? this script is not copyrighted as the last one. function...
1
by: skyson2ye | last post by:
Hi, guys: I have written a piece of code which utilizes Javascript in PHP to create a three level dynamic list box(Country, States/Province, Market). However, I have encountered a strange problem,...
2
by: yomadhu | last post by:
I created a dynamic form in javascript. Am unable to get those values in to php to display. I need all details. If i add 10 rows the i need to display those all values. Can any one help me for that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.