element.remove()
The DOM is organized in a tree of nodes, where each node has a value, along with a list of references to its child nodes. So element.parentNode.removeChild(element)
mimics exactly what is happening internally: First you go the parent node, then remove the reference to the child node.
As of DOM4, a helper function is provided to do the same thing: element.remove()
. This works in 96% of browsers (as of 2020), but not IE 11. If you need to support older browsers, you can:
- Remove elements via the parent node, as in the question,
- modify the native DOM functions, as in Johan Dettmar’s answer, or
- use a DOM4 polyfill.
Related Posts:
- jQuery document.createElement equivalent?
- How can I scroll to an element using jQuery?
- Include another HTML file in a HTML file
- How to resolve the C:\fakepath?
- Remove all child elements of a DOM node in JavaScript
- querySelector vs. getElementById
- JavaScript get element by name
- Find an element in DOM based on an attribute value
- React Error: Target Container is not a DOM Element
- scrollIntoView Scrolls just too far
- Difference between innerText, innerHTML and value?
- How do I set/unset a cookie with jQuery?
- Uncaught TypeError: Cannot read property ‘top’ of undefined
- How can I check if an element exists in the visible DOM?
- How can I select an element by name with jQuery?
- How can I add a class to a DOM element in JavaScript?
- Parse an HTML string with JS
- For loop for HTMLCollection elements
- jQuery count child elements
- How do I check if an element is hidden in jQuery?
- document.getElementbyId() returning null
- Retrieve the position (X,Y) of an HTML element
- Javascript: ‘window’ is not defined
- Invariant Violation: _registerComponent(…): Target container is not a DOM element
- Getting or changing CSS class property with Javascript using DOM style
- Cannot read property ‘style’ of undefined — Uncaught Type Error
- Invariant Violation: Objects are not valid as a React child
- How to get all place details from a Place Details Request using Google Places API Web Service?
- How do I check whether a checkbox is checked in jQuery?
- SyntaxError: Unexpected token o in JSON at position 1
- What does the function then() mean in JavaScript?
- Check if checkbox is checked with jQuery
- What is an unhandled promise rejection?
- What is a good regular expression to match a URL? [duplicate]
- Infinite Scrolling in Asp.Net MVC with jQuery / AJAX Issues
- What are the difference between $(document).bind(‘ready’, function) and $(document).ready(function() {})
- Getting “TypeError: failed to fetch” when the request hasn’t actually failed
- How to randomize (shuffle) a JavaScript array?
- Short circuit Array.forEach like calling break
- TypeError: Router.use() requires middleware function but got a Object
- What is the most efficient way to deep clone an object in JavaScript?
- Loading local JSON file
- How to check all checkboxes using jQuery?
- Accessing elements by type in JavaScript
- Uncaught TypeError: Cannot read property ‘value’ of null
- How to subtract date/time in JavaScript?
- Full-screen iframe with a height of 100%
- Change CSS of class in Javascript?
- Tainted canvases may not be exported
- Error: Cannot find module html
- Is it possible to append to innerHTML without destroying descendants’ event listeners?
- How to get the difference between two arrays in JavaScript?
- What happened to Lodash _.pluck?
- Simplest/cleanest way to implement a singleton in JavaScript
- Extract hostname name from string
- How to connect signalR from angularJs
- How can I copy to clipboard in HTML5 without using flash?
- how to get the value of a textarea in jquery?
- Maximum call stack size exceeded on npm install
- How to get the browser viewport dimensions?
- Getting HTML form values
- How to run a function when the page is loaded?
- RegEx: How can I match all numbers greater than 49?
- Multiple left-hand assignment with JavaScript
- Disabled href tag
- How to get child element by class name?
- TypeError getElementsByTagName is not a function issue
- Creating Accordion Table with Bootstrap
- Creating Multiple Modals on a Single Page
- Javascript, viewing [object HTMLInputElement]
- Why onbeforeunload event is not firing
- etect the Enter key in a text input field
- Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (
) - What’s the equivalent of Java’s Thread.sleep() in JavaScript?
- What do the args for Gutenberg subpackage “hooks” function “doAction” mean?
- Contact Form 7 – Give each checkbox a class?
- is_page not working when loading javascript with add_action in functions.php
- Hide reply button after moveForm is called
- How to add custom javascript into from custom plugin
- remove inline scripts from the_content() produced by plugins?
- How to set callback to execute after all the tinyMCE editors have been initialized?
- Correctly enqueue scripts of type=text/paperscript (PaperJs Library)
- useSelect second parameter
- Javascript in wordpress
- How do I make my wordpress post appear in a mouse hover preview?
- Do I just put the html in a page when enqueueng or do I also have to reference js file from the html page [closed]
- jQuery UI Tools: Scrollable: More than 1 instance possible?
- imporing HTML+javascript from REST API
- how to add a custom javascript file to our theme
- WordPress Guternberg Content For React Native
- Custom block update rendering when reused
- VisualComposer/WPBakery Page Editor: Is any JS event triggered after the Edition pop-in is shown?
- How do I add my own scripts to the add_action(‘wp_enqueue_scripts’) function?
- Post title and url not showing on google map marker [closed]
- Including js script at end of header
- Trigger a function during onload
- Embed Kajabi into WP
- Google AdSense Ad not showing up in WordPress Blog [closed]
- How to load JavaScript modules with script_loader_tag hook?
- How to securely set dynamic HTML content with JavaScript?