Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery, get an array of text()

I am trying to get and array of text after using the text function, having a ul list like

<ul>   <li>one     <ul>       <li>one one</li>       <li>one two</li>     </ul>   </li> </ul> 

then get something like

['one', 'one one', 'one two'] 
like image 452
Carlos Avatar asked May 15 '13 16:05

Carlos


People also ask

Does jQuery return an array?

The jQuery function always returns a jQuery object (that is based on an array), even if there are no elements that matches the selector.

How check value is present in array or not in jQuery?

inArray() method we can easily verify if values are available in an array of objects. The jQuery. InArray() is a built-in jquery method, used to find a given value within an array. If the value is available it returns its index, and if value not exists then it returns -1.


2 Answers

var array = $('li').map(function(){                return $.trim($(this).text());             }).get(); 

http://api.jquery.com/map/

Demo Fiddle --> http://jsfiddle.net/EC4yq/

like image 152
Mohammad Adil Avatar answered Sep 20 '22 04:09

Mohammad Adil


Try this:

$("li").map(function(){ return this.innerText }) 

or this:

$("li").toArray().map(function(i){ return i.innerText }) 
like image 25
Nakilon Avatar answered Sep 21 '22 04:09

Nakilon