Pergunta

var table = document.getElementById("table-body");
var row = table.insertRow(-1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
cell1.innerHTML = ingredient_name;
cell2.innerHTML = ingredient_amount;
cell3.innerHTML = ingredient_unit;
cell4.innerHTML = '<button type="button" class="close" aria-.hidden="true"onClick="$(this).closest(\'tr\').remove()">&times;</button>';   

How would I set the ID of a row or a table that I've just created?

Foi útil?

Solução

You can just set the id property of the new dom element reference

row.id='newid';
cell1.id='cellid1'
....

Outras dicas

add row in bottom Table and set dynamic ID. nice and neat way:

var newTr = $("<tr id="+e+"></tr>"); 
$("#TableID").append(NewTr);

my example :

function showimage(e) {
debugger;           
for (var i = 0; i < e.length; i++) {
var fd = new FormData();                
fd.append("ID", e[i].ID);
$.ajax({
url: "/AdminPanel/Pictures/ShowImage2",
type: 'POST',
contentType: false,
processData: false,
data: fd
}).then(function (e) {
var newTr = $("<tr id="+e+"></tr>");                    
var ImageTd = $('<td></td>');
var DeleteLinkTd = $('<td></td>');
var img = document.createElement("IMG");
img.src = "/AdminPanel/Pictures/ShowImage/"+e;
img.width = "100";
img.alt = "#" + e;
ImageTd.append(img);
var aTag = document.createElement('a');
aTag.setAttribute('onClick', "Delete("+e+");");
aTag.innerHTML = "Delete";
DeleteLinkTd.append(aTag);                    
newTr.append(ImageTd);
newTr.append(DeleteLinkTd);                    
$('#PicturesTable').append(newTr);                    
});}
$('#DivBulkUpload').dialog('close');
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top