How to Convert List of Elements in an Array using jQuery? Last Updated : 08 Oct, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 .map() methods ).Take the text value of each child and append that to the array.Example 1: This example uses .each() method to convert a list of elements in an array. html <!DOCTYPE HTML> <html> <head> <title> How to convert list of elements in an array using jQuery ? </title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script> <style> #ul { width: 80px; margin: 0 auto; } </style> </head> <body align="center"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <ul id="ul"> <li>GFG_1</li> <li>GFG_2</li> <li>GFG_3</li> </ul> <br> <button onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN" style="font-size: 24px; font-weight: bold; color: green;"> </p> <script> let el_up = document.getElementById('GFG_UP'); let el_down = document.getElementById('GFG_DOWN'); el_up.innerHTML = "Click on the button to get " + "the values of list in array."; function GFG_Fun() { let arr = []; $("ul li").each(function () { arr.push($(this).text()) }); el_down.innerHTML = '"' + arr.join('", "') + '"'; } </script> </body> </html> Output: Example 2: This example uses .map() method to convert a list of elements in an array. html <!DOCTYPE HTML> <html> <head> <title> How to convert list of elements in an array using jQuery ? </title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script> <style> #ul { width: 80px; margin: 0 auto; } </style> </head> <body align="center"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <ul id="ul"> <li>GFG_1</li> <li>GFG_2</li> <li>GFG_3</li> </ul> <br> <button onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN" style="font-size: 24px; font-weight: bold; color: green;"> </p> <script> let el_up = document.getElementById('GFG_UP'); let el_down = document.getElementById('GFG_DOWN'); el_up.innerHTML = "Click on the button to get " + "the values of list in array."; function GFG_Fun() { let arr = []; arr = $('li').map(function (j, element) { return $(element).text(); }).get(); el_down.innerHTML = '"' + arr.join('", "') + '"'; } </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to Convert List of Elements in an Array using jQuery? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How to reverse array of DOM elements using jQuery ? 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 2 min read How to convert a DOM nodelist to an array using JavaScript ? NodeList objects are the collection of nodes, usually returned by properties such as Node.childNodes and methods such as document.querySelectorAll(). Although NodeList is not an actual Array but it is possible to iterate over it with the help of forEach() method. NodeList can also be converted into 2 min read How to get all selected checkboxes in an array using jQuery ? In this article, we are going to discuss various methods to get all the selected checkboxes in an array using jQuery. Several ways of doing this in jQuery are explained in detail with their practical implementation using code examples. Table of Content Using the array.push() method with each() metho 4 min read How to convert Integer array to String array using JavaScript ? The task is to convert an integer array to a string array in JavaScript. Here are a few of the most used techniques discussed with the help of JavaScript. Approaches to convert Integer array to String array:Table of ContentApproach 1: using JavaScript array.map() and toString() methodsApproach 2: Us 2 min read How to convert Object's array to an array using JavaScript ? Given an array of objects and the task is to convert the object values to an array with the help of JavaScript. There are two approaches that are discussed below: Approach 1:We can use the map() method and return the values of each object which makes the array. Example: html<!DOCTYPE HTML> 2 min read Like