I already have add/delete rows dynamically in Javascript, there is no problem in adding rows, only having a problem with deleting rows.
For now, what I have made is when I want to delete a row it always deletes the last one, so I think I want if it only can be deleted the selected row, and I don't know how to modify the script to what I wanted be.
Here is the script:
var i=0;
function addRow()
{
i++;
m.r.value = i;
var tbl = document.getElementById('table');
var lastRow = tbl.rows.length;
var iteration = lastRow - 1;
var row = tbl.insertRow(lastRow);
var cellLeft = row.insertCell(0);
var textNode = document.createTextNode(iteration);
cellLeft.appendChild(textNode);
var cellRightSel1 = row.insertCell(1);
var sel = document.createElement('select');
sel.name = 'name' + iteration;
sel.setAttribute("onchange", "choosec(this);");
var item = new Option("","");
sel.options[sel.length] = item;
<?
while($data = mysql_fetch_array($result)){
?>
var item = new Option("<?=$data["Name"];?>","<?=$data["ID"];?>");
sel.options[sel.length] = item;
<? } ?>
cellRightSel1.appendChild(sel);
var cellRightSel2 = row.insertCell(2);
var sel = document.createElement('select');
sel.name = 'class' + iteration;
sel.setAttribute("onchange", "choosepoint(this);");
var item = new Option ("","");
sel.options[sel.length] = item;
<?
while($data = mysql_fetch_array($result_sub)){
?>
var item = new Option("<?=$data["Class"];?>","<?=$data["ID"];?>");
sel.options[sel.length] = item;
<? } ?>
cellRightSel2.appendChild(sel);
var cellRight = row.insertCell(3);
var div = document.createElement('div');
div.id = 'point' + iteration;
cellRight.appendChild(div);
}
function removeRow(){
var tbl = document.getElementById('table');
var lastRow = tbl.rows.length;
var rem = lastRow - 1;
if (lastRow > 2) tbl.deleteRow(rem);
}
Help is appreciated, thanks.
My solved problems:
function removeRow(t){
var i = t.parentNode.parentNode.rowIndex;
var tbl = document.getElementById('table');
var lastRow = tbl.rows.length;
var iteration = lastRow - 1;
tbl.deleteRow(i);
}
i? Inline event handler? Have you considered using jQuery? (Actually, before people like Raynos complain about suggesting jQuery, you can also improve that without jQuery - but why write more code than encessary)