| re: dynamically add a table row with onclick
cool2005 wrote:[color=blue]
> I tried to dynamically add/clone a <tr> from an existing <tr> to a
> <table> but the problem
> is that I need to have a "onclick" event handler in the <tr>.
>
>
> I have tried following
>
> A. approach 1.
>
> 1. find the <tr> that I will insert <tr>s right after it
> 2. get content (collection of outerHTML of those <tr>s to be
> inserted)
> 3. use insertAdjacentHTML to perform the inserting.
>
> This approach failed with a "runtime error" at step 3. The code is
> below[/color]
Inserting HTML into a table element to create a new TR will modify
the table's innerHTML property, which is read-only in IE:
"...the innerText and innerHTML properties of the table and tr
objects are read-only."
<URL:http://msdn.microsoft.com/workshop/author/tables/buildtables.asp>
The following also in regard to innerHTML:
"The property is read/write for all objects except the following, for
which it is read-only: COL, COLGROUP, FRAMESET, HTML, STYLE, TABLE,
TBODY, TFOOT, THEAD, TITLE, TR. The property has no default value."
<URL:http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/innerhtml.asp>
You can use innerHTML to modify the content of a cell (TR), but
generally it is better to use DOM methods unless you are just doing
something simple like replacing text.
As Martin suggested, the simplest way to create a copy of an existing
row is with cloneNode. The following example clones the last row in
the table and adds a new clone after it (tested in IE and Firefox).
I've included a document.all method, but I'm not sure how well
cloneNode is supported by older versions of IE that require it.
<script type="text/javascript">
function cloneRow(t){
if (document.getElementById( {
t = document.getElementById(t);
} else if (document.all) {
t = document.all[t];
} else { return;}
if (t.nodeName && 'TABLE' == t.nodeName){
var oldRow = t.rows[t.rows.length - 1];
var newRow = oldRow.cloneNode(true);
oldRow.parentNode.insertBefore(newRow, oldRow.nextSibling);
}
}
</script>
<input type="button" value="Add another row by cloning"
onclick="cloneRow('tableA')">
<table id="tableA">
<tr onclick="alert('hi from row ' + this.rowIndex)";>
<td>Click this to activate the tr onlcick</td>
</tr>
</table>
A word of caution: the element and its content are cloned exactly, so
if the tr has an id, the new row will have an identical id. You
should change it before adding it to the table.
Similarly, any enclosed form or other elements will be cloned exactly
too, so you may need to change their ID or NAME also as appropriate.
Lastly, when adding TR elements to a table, IE requires you to add
them to the tbody, not the table (HTML 4 conforming browsers will add
a tbody element regardless if it's been coded in the HTML). In the
code above, I've avoided the issue by adding the new row to the
parent of the old row.
Another approach would be to put the id on a tbody element and add
the new row to that.
...
if (t.nodeName && 'TBODY' == t.nodeName){
var oldRow = t.rows[t.rows.length - 1];
var newRow = oldRow.cloneNode(true);
t.insertBefore(newRow, oldRow.nextSibling);
}
...
<table>
<tbody id="tableA">
<tr onclick="alert('hi from row ' + this.rowIndex)";>
...
[...]
--
Rob |