How to reverse array of DOM elements using jQuery ? Last Updated : 15 May, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an array of DOM elements, the task is to reverse this array using jQuery. There are two approaches that can be taken to achieve this: Approach 1: Use inbuilt jQuery methods such as get(), reverse(), and each(). Firstly, select all the required DOM elements using the get() method which returns a JavaScript array. Then, the native JavaScript method reverse() is used to reverse the array of DOM elements. Finally, each() method iterates over each DOM element of the reversed array. Example: In this example, we will use the above approach. HTML <!DOCTYPE html> <html> <head> <!-- jQuery --> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> </script> <title> Reverse array of DOM elements using jQuery </title> <!-- Basic inline styling --> <style> body { text-align: center; } p { font-weight: bold; font-size: 1.5rem; } h1 { color: green; font-size: 2.5rem; } button { cursor: pointer; } </style> </head> <body> <h1>GeeksforGeeks</h1> <p>Geek 1</p> <p>Geek 2</p> <p>Geek 3</p> <p>Geek 4</p> <p>Geek 5</p> <script type="text/javascript"> $($("p").get().reverse()).each(function (i) { $(this).text("Geek " + ++i); }); </script> </body> </html> Output: Approach 2: Using a small jQuery plugin. The plugin jQuery.fn.reverse = [].reverse can be used to reverse an array of DOM elements when coupled with each() method as discussed in the previous approach. Example: In this example, we will use the above approach. HTML <!DOCTYPE html> <html> <head> <!-- jQuery --> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> </script> <title> Reverse array of DOM elements using jQuery </title> <!-- Basic inline styling --> <style> body { text-align: center; } p { font-weight: bold; font-size: 1.5rem; } h1 { color: green; font-size: 2.5rem; } button { cursor: pointer; } </style> </head> <body> <h1>GeeksforGeeks</h1> <p>Geek 1</p> <p>Geek 2</p> <p>Geek 3</p> <p>Geek 4</p> <p>Geek 5</p> <script type="text/javascript"> $.fn.reverse = [].reverse; $("p").reverse().each(function (i) { $(this).text("Geek " + ++i); }); </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to reverse array of DOM elements using jQuery ? R rajatsandhu2001 Follow Improve Article Tags : Web Technologies JQuery jQuery-Questions Similar Reads How to Convert List of Elements in an Array using jQuery? Given an HTML document containing a list of elements, the task is to get the values of the HTML list and convert it into an array using JavaScript. ApproachMake a list(Ordered/ Unordered).Select the parent of <li> element and call an anonymous function for every child of parent( .each() or .ma 2 min read How to Remove First and Last Element from Array using JavaScript? Removing the first and last elements from an array is a common operation. This can be useful in various scenarios, such as data processing, filtering, or manipulation. Example:Input: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]Output: [ 2, 3, 4, 5, 6, 7, 8, 9 ]Removing first and last element in array can be do 2 min read How to get elements in reverse order of an array in PHP ? An array is a collection of elements stored together. Every element in an array belong to a similar data type. The elements in the array are recognized by their index values. The elements can be subjected to a variety of operations, including reversal. There are various ways to reverse the elements 4 min read How to remove specific elements from the left of a given array of elements using JavaScript ? In this article, we will learn How to remove specific elements from the left of a given array of elements using JavaScript. We have given an array of elements, and we have to remove specific elements from the left of a given array. Here are some common approaches: Table of Content Using splice() met 2 min read How to use array with jQuery ? An array is a linear data structure. In JavaScript, arrays are mutable lists with a few built-in methods, we can define arrays using the array literal. The arrays can be used in the same way in jQuery as used in JavaScript. Syntax And Declaration:let arr1=[];let arr2=[1,2,3];let arr2=["India","usa", 1 min read Like