
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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>
Advertisements