View Full Version : DOM: referencing rows for removal.

11-27-2005, 10:57 PM

I've had to come up with a side-script to delete a row based on its contents no matter which row it is.

Now my whole program needs to be changed and I want to know if there is any way to reference a row for deletion, like using the this operator, for instance.

i.e.: I want to hit an image inside a row and the result should be the removal of that very row with all its contents.

PS: this doesn't seem to work with row removal commands. It says the object does not support the current method.

11-28-2005, 11:00 AM
Play with parentNodes:

<script type="text/javascript">
function removeThisRow(obj){
var myRow=obj.parentNode.parentNode;

... no matter what
... no matter what
... no matter what but not a parentNode for img
<img src="removal.gif" onclick = removeThisRow(this)>
... no matter what
... no matter what
... no matter what

11-28-2005, 04:44 PM
With a small modification, that could allow for any number of intermediate parent elements:

function removeThisRow(obj)
var myRow = obj.parentNode;
while(myRow.parentNode.nodeName.toLowerCase() != 'tr')
myRow = myRow.parentNode;

11-28-2005, 05:03 PM
Yes, brothercake, sorry, suprisingly I had forgotten about that, even I usually use the code exactly in that manner... Must be the entire day tiresome's fault...:)

11-28-2005, 08:23 PM
Thank you all. Surprisingly, I haven't even thought of using removeChild with parentNode . Now I feel stupid. :B

Things are working fine now, thanks again. =D

The problem I talked about before was that I was using the deleteRow[0] handler which requires the row index. The problem appears when having to delete more than one row, because while the command could work the first time, all the indexes change, and if the removal function itself doesn't change, then it's rendered useless.

I could work that problem out by playing with indexes as variables, but it becomes next to impossible when having to deal with 13200+ item. ^^;

Anyway, this and parentNode, thank you. :thumbsup:
Special thanks for brothercake for the little trick. :D