How to pass data to a new line created by javascript

Asked

Viewed 111 times

0

I am creating a function to save a form directly in a table, the table rows contain an onclick function that takes them to 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 I insert attributes into the new row created by the save option, in case I have to insert the html effect onclick="editar(this)";

var linha1 = document.getElementById("tabela").insertRow (-1);
  • Your question is not yet clear to me. You can join HTML and the rest of the code to see an example working?

  • Sérgio is seeing the function foo(), so follow along with me I will try to make it clear, if (line == null) { var line1 = Document.getElementById("table"). insertRow(-1);, know this Insert Row(-1) it inserts a new line only that without any property, I need to pass this new line created <tr> an onclick event="edit" or a class="effect", for the line to be in agreement with the others.

1 answer

1


That 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 put an Anchor even, 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);
  • That’s exactly what I was looking for, thank you, it really helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.