48

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']
2
  • 2
    Please post the jQuery you've tried. Commented May 15, 2013 at 16:41
  • I was working with jstree, cause of that I dont paster my code cause it is too long, however, all ideas from here helpe a lot on how to solve my issue. Thanks to all Commented May 15, 2013 at 16:56

7 Answers 7

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

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

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

Sign up to request clarification or add additional context in comments.

3 Comments

You may want to chain a call to .toArray().
what does get() method do? BTW We can do $('li').toArray().map((li) => { return li.innerText.trim(); })
@mangatinanda @TrueWill i think .get and .toArray do the same thing api.jquery.com/get
13

Try this:

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

or this:

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

Comments

9

You just want to get the text from all the li's?

var resultArray = [];

$('li').each(function(){
    resultArray.push($(this).text());
});

Comments

3

Several of the answers ignore the OP's desired format.

This will give you what you want.

$('li').map(function(){
    var $clone = $(this).clone();
    $clone.children('*').remove();
    return $.trim($clone.text());
}).get()

Fiddle: http://jsfiddle.net/pE9PR/

Comments

2

You could do something like

var children = $(listholder).children();
var array = [];
var i = 0;
$.each(children, function(key, value){
array.push($(this).text());
});

Comments

0

The accepted answer actually not answer the question, OP want firstChild text

['one', 'one one', 'one two']

but accepted answer give textContent

['one\n\none one\none two', 'one one', 'one two']

There are two method to answer, first using jQuery contents()

var array = $('li').map(function() {
  return $(this).contents()[0].textContent.trim()
}).get();

console.log(array)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
  <li>one
    <ul>
      <li>one one</li>
      <li>one two</li>
    </ul>
  </li>
</ul>

second, using plain js firstChild

var array = Array.from(document.querySelectorAll('li'))
  .map(i => i.firstChild.textContent.trim())
console.log(array)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
  <li>one
    <ul>
      <li>one one</li>
      <li>one two</li>
    </ul>
  </li>
</ul>

Comments

0

This gets the job done regardless of the tag names. Select your parent.

It gives an array of strings with no duplications for parents and their children.

$('parent')
.find(":not(iframe)")
.addBack()
.contents()
.filter(function() {return this.nodeType == 3;})
.map((i,v) => $(v).text())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.