
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Is there a DOM function which deletes all elements between two elements in JavaScript?
Let’s say the following are our elements −
<p>My Name is John</p> <p>My Name is David</p> <p>My Name is Bob</p> <p>My Name is Mike</p> <p>My Name is Carol</p> <footer>END</footer>
We need to remove the
element and its content. The
elements are in between the START and END.
To remove elements between two elements, use the concept of remove(). Following is the code −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initialscale=1.0"> <title>Document</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> </head> <body> <nav>START</nav> <p>My Name is John</p> <p>My Name is David</p> <p>My Name is Bob</p> <p>My Name is Mike</p> <p>My Name is Carol</p> <footer>END</footer> <script> const startingPoint = document.querySelector("nav"); const endingPoint = document.querySelector("footer"); while (startingPoint.nextElementSibling && startingPoint.nextElementSibling !== endingPoint) { startingPoint.nextElementSibling.remove(); } </script> </body> </html>
To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.
Output
This will produce the following output −
- Related Articles
- Remove all child elements of a DOM node in JavaScript?
- Find all elements in array which have at-least two greater elements in C++
- How can I remove all child elements of a DOM node in JavaScript?
- X and Y are the two elements having similar properties which obey Newlands’ law of octaves. How many elements are there in-between X and Y?
- Finding matches in two elements JavaScript
- Function to choose elements randomly in JavaScript
- JavaScript: How to loop through all DOM elements on a page and display result on console?
- Count number of elements between two given elements in array in C++
- Python – Nearest occurrence between two elements in a List
- How to sum all elements in a nested array? JavaScript
- Sum all similar elements in one array - JavaScript
- Finding sum of all unique elements in JavaScript
- Can all array elements mesh together in JavaScript?
- Reducing array elements to all odds in JavaScript
- Currified function that multiples array elements in JavaScript

Advertisements