Web Platform
HTML Table Add Row - innerHTML vs. DOM vs. cloneNode
하얀전쟁^^
2010. 8. 2. 11:02
Example 1: innerHTML
DOM table methods using innerHTML to fill in cells
function addRowInnerHTML(tblId) { var tblBody = document.getElementById(tblId).tBodies[0]; var newRow = tblBody.insertRow(-1); var newCell0 = newRow.insertCell(0); newCell0.innerHTML = ''; var newCell1 = newRow.insertCell(1); newCell1.innerHTML = 'cell 1 - just plain text'; }
Example 2: DOM
DOM table methods using DOM to fill in cells
function addRowDOM(tblId) { var tblBody = document.getElementById(tblId).tBodies[0]; var newRow = tblBody.insertRow(-1); var newCell0 = newRow.insertCell(0); var newInput = document.createElement('input'); newInput.type = 'text'; newInput.value = 'cell 0 - text box'; newInput.style.color = 'blue'; newCell0.appendChild(newInput); var newCell1 = newRow.insertCell(1); newCell1.appendChild(document.createTextNode('cell 1 - just plain text')); }
Example 3: DOM Clone
cloneNode to clone an existing row
function addRowClone(tblId) { var tblBody = document.getElementById(tblId).tBodies[0]; var newNode = tblBody.rows[0].cloneNode(true); tblBody.appendChild(newNode); }