miércoles, 15 de julio de 2009

Agregar elementos a un a tabla HTML en Firefox e Internet Explorer

Para lograr agregar elementos dinámicamente a una tabla y que funcione en FireFox e Internet Explorer se debe crear la tabla de la siguiente manera:

<html>
<body>
<script language="javascript">
function function2(){
var myBody = document.getElementById('mainbody');

var myElement = document.createElement('tr');
var tds = document.createElement('td');
var celda = document.createTextNode('celda 1');

tds.appendChild(celda);
myElement.appendChild(tds);

celda = document.createTextNode('celda 2');
tds = document.createElement('td');
tds.appendChild(celda);
myElement.appendChild(tds);

myBody.appendChild(myElement);

}
</script>
<body id="myBody">
<table>
<thead>
<tr>
<th>1</th>
<th>2</th>
</tr>
</thead>
<tbody id="mainbody">
</tbody>
</table>
<button onClick="function2();">Put trs</button>
</body>
</html>


Si la tabla no es creada con un TBODY, en IE no funciona apropiadamente y es necesario agregar los elementos usando el atributo innerHTML de la tabla

Mis fotos en TrekEarth