Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery collect value of list items and place in array

If I have the following HTML:

<ul>
  <li>List 1</li>
  <li>list 2</li>
  <li>list 3</li>
</ul>

Can I get the text content from the the <li>'s and place them in array using javascript?

like image 292
sea_1987 Avatar asked Jan 31 '11 21:01

sea_1987


People also ask

How do you find the value of an element in an array?

prototype. values() The values() method returns a new array iterator object that iterates the value of each index in the array.

How to use jQuery inArray?

The jQuery inArray() method is used to find a specific value in the given array. If the value found, the method returns the index value, i.e., the position of the item. Otherwise, if the value is not present or not found, the inArray() method returns -1. This method does not affect the original array.


1 Answers

var arr = $("li").map(function() { return $(this).text() }).get();
  • The map()(docs) method creates a jQuery object populated with whatever is returned from the function (in this case, the text content of each <li> element).

  • The get()(docs) method (when passed no argument) converts that jQuery object into an actual Array.

like image 139
Luke Avatar answered Nov 12 '22 08:11

Luke