Ver Mensaje Individual
  #5 (permalink)  
Antiguo 02/02/2014, 01:13
darkcl0wn
 
Fecha de Ingreso: febrero-2014
Mensajes: 32
Antigüedad: 10 años, 3 meses
Puntos: 0
Respuesta: Añadir/Eliminar filas de tablas html con javascript, gran problema

Hola djaevi, gracias por tu codigo, estuve metiendole mano y funciona realmente, pero no fue mi solucion ya que al input queria agregarle un onFocus y desde las propiedades donde se creaba el input es decir
Código:
var input = document.createElement("input");
        input.type = "text";
        input.className = "urlresp";
        input.value = "Usuario";
        input.style.height = "20px";
        input.style.width = "100px";
ahi no supe como agregarselo, reitero, se poco y nada de javascript, pero a continuacion voy a dejar el primer codigo correjido por PHPeros que funciona, tu codigo y el ultimo que use como aporte para que alguien que tenga un problema igual o similar al mio pueda usar estos codes, ahi van...

Este es el primer codigo corregido por PHPeros

Código HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script>
        function myCreateFunction() {
            var table = document.getElementById("myTable"); {
                var row = table.insertRow(0);
                var cell1 = row.insertCell(0);
                var cell2 = row.insertCell(1);
                cell1.innerHTML = '<input type="text">';
                cell2.innerHTML = '<input type="text" id=txt>';
                cell1.style.height = "20px";
                cell1.style.width = "100px";
                cell2.style.height = "20px";
                cell2.style.width = "30px";
                txt.style.width = "30px";
            }
        }

        function myDeleteFunction() {
            document.getElementById("myTable").deleteRow(0);
        }
    </script>
</head>

<body>
    <button onclick="myCreateFunction()" type="button">
        <img src="Boton.png">
    </button>
    <button onclick="myDeleteFunction()" type="button">
        <img src="Boton2.png">
    </button>
    <p>&nbsp;</p>
    <table id="myTable" border="0">
        <tr>
            <td style="height: 20px; width: 100px;">
                <input name="urlresp" type="text" id="urlresp" onfocus="this.value=''" value="Url respuesta">
            </td>
            <td style="height: 20px; width: 30px;">
                <input name="ptss" type="text" id="txt" style="width: 30px;" onfocus="this.value=''" value="0">
            </td>
        </tr>
    </table>
</body>

</html> 

Este codigo es el que proporciono djaevi, lo modifique un poco acuerdo a mis necesidades

Código HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Agregar y quitar celdas</title>
    <script>

        function myCreateFunction() {

            var table = document.getElementById("myTable");
            var row = table.insertRow(0);
            var cell1 = row.insertCell(0);
            var cell2 = row.insertCell(1);
            // CREO UN ELEMENTO DEL TIPO INPUT CON document.createElement("NOMBRE TAG HTML QUE QUIERO CREAR");



            var input = document.createElement("input");
            input.type = "text";
            input.className = "urlresp";
            input.value = "Usuario";
            input.style.height = "20px";
            input.style.width = "100px";

            // Creo un segundo elemento Input


            var input2 = document.createElement("input");
            input2.type = "text";
            input2.className = "ptss";
            input2.value = "0";
            input2.autofocus;
            input2.style.height = "20px";
            input2.style.width = "30px";


            // CON EL METODO appendChild(); LOS AGREGO A LA CELDA QUE QUIERO
            cell1.appendChild(input);
            cell2.appendChild(input2);

            document.getElementById("input2").onFocus();

        }



        function myDeleteFunction() {
            document.getElementById("myTable").deleteRow(0);
        }
    </script>
</head>

<body>
    <table id="myTable" border="0">
        <tr>

        </tr>
    </table>

    <button onclick="myCreateFunction()" type="button">Crear</button>
    <button onclick="myDeleteFunction()" type="button">Borrar</button>
</body>

</html> 
Este otro es el ultimo que realmente me sirvio ya que pude agregarle el evento onFocus, pero no supe agregarle un boton para eliminar las celdas, pero da igual no importa.

Código HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Agregar y quitar celdas</title>
    <script>
        function vaciar(control) {
            control.value = '';
        }

        function verificarEntrada(control) {
            if (control.value == '')
                alert('Debe ingresar datos');
        }


        var orden = 1;

        function clonarNodos() {
            var id = document.getElementById("myTable");
            var nuevos = id.cloneNode(true);
            nuevos.style.id = 'enlaces' + orden;
            orden++;
            id = document.getElementById("NewTable");
            id.appendChild(nuevos);
        }

        function myDeleteFunction() {
            document.getElementById("myTable").deleteRow(0);
        }
    </script>
</head>

<body>
    <table id="myTable" border="0">
        <tbody>
            <tr>
                <td>
                    <input value="Usuario" style="height: 20px; width: 100px;" id="urlresp" type="text" onFocus="vaciar(this)">
                    <input value="0" style="height: 20px; width: 30px;" class="ptss" type="text" onFocus="vaciar(this)">
                    </br>
                </td>
        </tbody>
    </table>

    <table id="NewTable" border="0">
    </table>
    <button onclick="javascript:clonarNodos()" value="Clonar nodos">Crear</button>
</body>

</html>