P
Prince Al
Hi,
I have been messing with some Javascript over the last couple of days.
What I want to achieve inititally is to be able to have a set of
fucntions that adds/removes cells from a table row and add/delete
tables from the page. I have managed to hack some code together to add/
remove cells that works in FF, but when I try to run the code in IE,
it breaks and being somewhat of a JS novice, I have no idea why! If
anyone could give me some pointers, I would be most greatful My
code is below.
Thanks in advance
Tim
<html> <head>
<SCRIPT LANGUAGE="JavaScript">
function addCellToRow(row_id,cell_id,side) {
alert('test');
var datetime = new Date();
var time = datetime.getTime();
var addLeft="addCellToRow(\'"+row_id+"\',\'"+time+"\',\'left\');";
var addRight="addCellToRow(\'"+row_id+"\',\'"+time+"\',\'right
\');";
var rem="removeCellFromRow(\'"+row_id+"\',\'"+time+"\');";
var tbl = document.getElementById('table');
var row = document.getElementById(row_id);
var cell = document.getElementById(cell_id);
var new_break = document.createElement("p");
var new_cell = document.createElement("td");
var new_form = document.createElement("form");
var new_add_right = document.createElement("button");
var new_add_left = document.createElement("button");
var new_remove = document.createElement("button");
var add_right_text = document.createTextNode('Add right');
var add_left_text = document.createTextNode('Add Left');
var remove_text = document.createTextNode('Remove Column');
new_add_left.setAttribute("onclick",addLeft);
new_add_right.setAttribute("onclick",addRight);
new_remove.setAttribute("onclick",rem);
new_add_left.setAttribute("type","button");
new_add_right.setAttribute("type","button");
new_remove.setAttribute("type","button");
new_cell.setAttribute("id",time);
new_form.appendChild(new_add_left);
new_form.appendChild(new_add_right);
new_form.appendChild(new_break);
new_form.appendChild(new_remove);
new_add_left.appendChild(add_left_text);
new_add_right.appendChild(add_right_text);
new_remove.appendChild(remove_text);
new_cell.appendChild(new_form);
if (side == "right") {
row.insertBefore(new_cell,cell.nextSibling);
} else {
row.insertBefore(new_cell,cell);
}
}
function removeCellFromRow(row_id,cell_id)
{
var row = document.getElementById(row_id);
var cell = document.getElementById(cell_id);
//var lastRow = tbl.rows.length;
row.deleteCell(cell.cellIndex);
}
</script>
</head>
<BODY>
<form action="tableaddrow_nw.html" method="get">
<table border="1" id="table">
<tr id=000000>
<td id=0>
<input type="button" value="Add Left"
onclick="addCellToRow('000000','0','left');" /><br>
<input type="button" value="Add Right"
onclick="addCellToRow('000000','0','right');" /><br>
<input type="button" value="Remove"
onclick="removeCellFromRow('000000','0');" /><br>
</td>
</tr>
</table>
</form>
</body>
</html>
I have been messing with some Javascript over the last couple of days.
What I want to achieve inititally is to be able to have a set of
fucntions that adds/removes cells from a table row and add/delete
tables from the page. I have managed to hack some code together to add/
remove cells that works in FF, but when I try to run the code in IE,
it breaks and being somewhat of a JS novice, I have no idea why! If
anyone could give me some pointers, I would be most greatful My
code is below.
Thanks in advance
Tim
<html> <head>
<SCRIPT LANGUAGE="JavaScript">
function addCellToRow(row_id,cell_id,side) {
alert('test');
var datetime = new Date();
var time = datetime.getTime();
var addLeft="addCellToRow(\'"+row_id+"\',\'"+time+"\',\'left\');";
var addRight="addCellToRow(\'"+row_id+"\',\'"+time+"\',\'right
\');";
var rem="removeCellFromRow(\'"+row_id+"\',\'"+time+"\');";
var tbl = document.getElementById('table');
var row = document.getElementById(row_id);
var cell = document.getElementById(cell_id);
var new_break = document.createElement("p");
var new_cell = document.createElement("td");
var new_form = document.createElement("form");
var new_add_right = document.createElement("button");
var new_add_left = document.createElement("button");
var new_remove = document.createElement("button");
var add_right_text = document.createTextNode('Add right');
var add_left_text = document.createTextNode('Add Left');
var remove_text = document.createTextNode('Remove Column');
new_add_left.setAttribute("onclick",addLeft);
new_add_right.setAttribute("onclick",addRight);
new_remove.setAttribute("onclick",rem);
new_add_left.setAttribute("type","button");
new_add_right.setAttribute("type","button");
new_remove.setAttribute("type","button");
new_cell.setAttribute("id",time);
new_form.appendChild(new_add_left);
new_form.appendChild(new_add_right);
new_form.appendChild(new_break);
new_form.appendChild(new_remove);
new_add_left.appendChild(add_left_text);
new_add_right.appendChild(add_right_text);
new_remove.appendChild(remove_text);
new_cell.appendChild(new_form);
if (side == "right") {
row.insertBefore(new_cell,cell.nextSibling);
} else {
row.insertBefore(new_cell,cell);
}
}
function removeCellFromRow(row_id,cell_id)
{
var row = document.getElementById(row_id);
var cell = document.getElementById(cell_id);
//var lastRow = tbl.rows.length;
row.deleteCell(cell.cellIndex);
}
</script>
</head>
<BODY>
<form action="tableaddrow_nw.html" method="get">
<table border="1" id="table">
<tr id=000000>
<td id=0>
<input type="button" value="Add Left"
onclick="addCellToRow('000000','0','left');" /><br>
<input type="button" value="Add Right"
onclick="addCellToRow('000000','0','right');" /><br>
<input type="button" value="Remove"
onclick="removeCellFromRow('000000','0');" /><br>
</td>
</tr>
</table>
</form>
</body>
</html>