How to pass data to a new line created by javascript

0

I'm creating a function to save a form directly to a table, the table rows contain an onclick function that takes the function in javascript

function xpto() {
    if (linha == null) {
        var linha1 = document.getElementById("tabela").insertRow(-1);
        linha1.insertCell(0).innerHTML = document.getElementById("oT").value;
        linha1.insertCell(1).innerHTML = document.getElementById("tColeta").value;
        linha1.insertCell(2).innerHTML = document.getElementById("tEntrega").value;
        linha1.insertCell(3).innerHTML = document.getElementById("pEntrega").value;
        linha1.insertCell(4).innerHTML = document.getElementById("trans").value;
        linha1.insertCell(5).innerHTML = document.getElementById("mod").value;

    } else {
        linha.cells[0].innerHTML = document.getElementById("oT").value;
        linha.cells[1].innerHTML = document.getElementById("tColeta").value;
        linha.cells[2].innerHTML = document.getElementById("tEntrega").value;
        linha.cells[3].innerHTML = document.getElementById("pEntrega").value;
        linha.cells[4].innerHTML = document.getElementById("trans").value;
        linha.cells[5].innerHTML = document.getElementById("mod").value;


    }
    linha = null;
    mostrarTabela();
}

How do I insert attributes in the new line created by the save option, in case I have to insert the html effect onclick="editar(this)";

var linha1 = document.getElementById("tabela").insertRow (-1);
    
asked by anonymous 30.05.2016 / 23:32

1 answer

1

In this way I believe it works:

var linha1 = document.getElementById("tabela").insertRow (-1);
linha.setAttribute('class', 'row');
linha.setAttribute('onclick','editar(this)');

If you want to put in a specific column, you can create the element in this case I put an even anchor, but it can be a span or whatever you want:

cell = linha.insertCell(index);
elemA = document.createElement('a');
elemA.setAttribute('href', '#');    
elemA.setAttribute('onclick','editar(this)');
elemA.setAttribute('title', 'Alterar');
elemA.innerHTML ="<img src='imagens/editar.jpg' name='editar' id='id' class='atualizar'>";  
cell.appendChild(elemA);
    
31.05.2016 / 01:53