Im trying to remove a Div from the DOM via an <a>
tag nested within it.
I guess what I am looking for is the pure Javascript version of jQuery's
$('div').remove()
Here's the html set up
<div> <a href = "#" onClick = "w/e()">Click me to remove the parent div</a></div>
Thanks ahead of time. :D
Approach: Select the HTML element which need to remove. Use JavaScript remove() and removeChild() method to remove the element from the HTML document.
Summary. Removing JavaScript Array items is important to managing your data. There is not a single 'remove' method available, but there are different methods and techniques you can use to purge unwanted array items.
The JavaScript delete operator removes a property from an object; if no more references to the same property are held, it is eventually released automatically.
The list items are added or removed using JavaScript functions addItem() and removeItem(). The list items are created using document. createElement() method and to create a text node, document. createTextNode() method is used and then this node is appended using appendChild() method.
You could define this function
function remove(element) {
element.parentNode.removeChild(element);
}
and use it as
<div>
<a href="#" onClick="remove(this.parentNode)">...</a>
</div>
Reference: Node.parentNode
, Node.removeChild
Further notes:
<button>
instead of a link (<a>
) for this kind of behaviour. A link has a distinct semantic meaning, it has to link somewhere. You can use CSS to style the button accordingly.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With