- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I remove all elements except the first one using jQuery?
To remove all elements except the first one, use the remove() method with the slice() method in jQuery. You can try to run the following code to remove all elements except for first one using jQuery −
Example
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $(".button1").click(function(){ $("span").remove(); }); $(".button2").click(function(){ $('span').slice(1).remove(); }); }); </script> </head> <body> <button class="button2">Hide all, except first element</button> <button class="button1">Hide all the elements</button> <span> <p>This is demo text.</p> <p>This is demo text.</p> </span> <span> <p>This is demo text.</p> <p>This is demo text.</p> </span> <span> <p>This is demo text.</p> <p>This is demo text.</p> </span> </body> </html>
- Related Articles
- How to find all elements in a given array except for the first one using JavaScript?
- How can I remove everything inside of a using jQuery?
- How to remove all the elements from DOM using element ID in jQuery?
- Remove all except the first character of a string in MySQL?
- How to remove all objects except one or few in R?
- How to click anywhere on the page except one element using jQuery?
- How to remove all style attributes using jQuery?
- How to remove all CSS classes using jQuery?
- How to remove all CSS classes using jQuery?
- How can I remove all child elements of a DOM node in JavaScript?
- How can I bind all events on a DOM element using jQuery?
- How can I remove all empty values when I explode a string using PHP?
- How to remove all the attributes of an HTML element using jQuery?
- How can I write a try/except block that catches all Python exceptions?
- How to remove all child nodes from a parent using jQuery?

Advertisements