views:

116

answers:

2

How to remove parent element and all the respective nodes using plain javascript. I m not using any Jquery or other library. Its like u are havin an element and when user click on that u want to remove the parent to parent element.(also the respective chlds nodes of that)

EDIT

<table id='table'>
        <tr id='id'>
            <td>
                Mohit
            </td>

            <td>
                    23
            </td>   
            <td >
                <span onClick="edit(this)">Edit</span>/<span onClick="delete_row(this)">Delete</span>
            </td>   
            <td style="display:none;">
                <span onClick="save(this)">Save</span>
            </td>   
        </tr>   
    </table>    

Now

 function delete_row(e)
            {
                e.parentNode.parentNode.removeChild(e.parentNode);

            }

Will remove only last td. Why cant i remove directly tr cause e.parentNode.parentNode.getAttribute('id')

will return id of row and using this is there any function like remove() delete()...???

+4  A: 
node.parentNode.parentNode.removeChild(node.parentNode)

Edit: You need to to delete parent of parent, so add one more .parentNode

node.parentNode.parentNode.parentNode.removeChild(node.parentNode.parentNode)
S.Mark
@S.Mark pls chk the edit part..
piemesons
@piemesons, you just need one more .parentNode (its parent of parent)
S.Mark
thnks S.Mark...
piemesons
+1  A: 

Change your function like this:

function delete_row(e)
{
    e.parentNode.parentNode.parentNode.removeChild(e.parentNode.parentNode);
}
Jakob Kruse
@Jakob Kruse pls chk the edit part...!!
piemesons
The HTML you pasted has you clicking a span, in a cell, in a row, so you would want to delete the parent of the parent. Just add another ".parentNode".
Jakob Kruse
e.parentNode.parentNode.parentNode.removeChild(e.parentNode.parentNode);
Jakob Kruse
thnku i worked...
piemesons
Great. I updated my answer to reflect the comments
Jakob Kruse