How to Convert List of Elements in an Array using jQuery? Last Updated : 08 Oct, 2024 Comments Improve Suggest changes 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 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 add a list elements within an unordered list element using jQuery ? Here in this article, we will see that how we can add a list to the Unordered List using jQuery. So the function which we had used to append the items in the list are - <script> $(document).ready(function() { $('.btn').click(function(){ var content = $('#addList').val(); var fixingContent=' 1 min read How to create a string by joining the elements of an array in JavaScript ? Given an array containing array elements and here we will join all the array elements to make a single string. To join the array elements we use arr.join() method. There are two methods by which we can create a string by joining the elements of an array: Table of Content Using arr.join() MethodUsing 2 min read How to Create an Array of Object Literals in a Loop using JavaScript? Creating arrays of object literals is a very frequent activity in JavaScript, as often when working with data structures, several objects are needed. This article will allow your hand through different ways of coming up with an array of object literals in a loop in JavaScript. Here are different app 5 min read How to convert array of strings to array of numbers in JavaScript ? Converting an array of strings to an array of numbers in JavaScript involves transforming each string element into a numerical value. This process ensures that string representations of numbers become usable for mathematical operations, improving data handling and computational accuracy in applicati 4 min read Like