...

View Full Version : DOM: referencing rows for removal.



KingRoach
11-27-2005, 10:57 PM
Hello.

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.

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


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


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

brothercake
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;
}
myRow.parentNode.removeChild(myRow);
}

Kor
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...:)

KingRoach
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum