How to reverse array of DOM elements using jQuery ? Last Updated : 15 May, 2023 Comments Improve Suggest changes 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 change the element id using jQuery ? The jQuery methods are used to change the element ID which are described below: jQuery attr() Method: This method set/return attributes and values of the selected elements. If this method is used to return the attribute value, it returns the value of first selected element. If this method is used to 3 min read How to iterate through child elements of a div using jQuery ? jQuery Selector can be used to find (select) HTML elements from the DOM. Once an element is selected, the jQuery children() method is called to find all the child elements of the selected element. To loop through the child elements, the jQuery each() method is used as demonstrated in the following e 3 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 How to sort a list alphabetically using jQuery ? Given a list of elements, The task is to sort them alphabetically and put each element in the list with the help of jQuery. jQuery text() Method: This method set/return the text content of the selected elements. If this method is used to return content, it provides the text content of all matched el 3 min read How to Reverse Custom Counters using CSS ? Counters in CSS are basically variables which can be used for numbering and values of CSS counters may be incremented by CSS rules. Follow the article, you will get to know 2 methods to reverse custom counter using CSS. We will be going to use the following two approaches to reverse counters: In app 3 min read TypeScript Array reverse() Method The Array.reverse() method in TypeScript is used to reverse the elements of an array in place, modifying the original array. It rearranges the elements such that the first element becomes the last, the second element becomes the second-to-last, and so on.Syntax:array.reverse(); Parameter: This metho 2 min read Like