Ghhihihihih
Ghhihihihih

Reputation: 27

How to delete parent a element completely

if this is my div element

<a href="sdfsfsf"><p class="quotes">blablabla</p></a>

If it is not homepage, how can you delete parent a for this quotes

i could not configure this code for my element:

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

js program must be automatically loaded also when page is loaded and it is not home page, it should delete a element

Upvotes: 0

Views: 59

Answers (2)

Alnitak
Alnitak

Reputation: 339786

To remove the immediate parent of a child element e, leaving the child still in the DOM (but reparented to its original grandparent), use .replaceChild:

e.parentNode.parentNode.replaceChild(e, e.parentNode);

Upvotes: 1

remix23
remix23

Reputation: 2984

If you want to leave p in place you need to add it to its parent's container first:

function delete_row(e) {
    var parent = e.parentNode;
    var grandParent = parent.parentNode;
    // remove e from its parent
    e.remove();
    // insert e into its grandParent, before parent
    grandParent.insertBefore(e, parent);
    // remove parent
    parent.remove();
}

But the same can be accomplished by the solution of @Alnitak above.

Today I've learned :)

Upvotes: 0

Related Questions