0

I need to get the values of a bunch of list items into an array.

This is what I'm trying at the moment:

var array = $('li').each(function () {
  $(this).html();
};

4 Answers 4

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

You can do it with .each(), but you have to actually build an array explicitly:

var array = [];
$('li').each(function() { array.push($(this).html()); });
Sign up to request clarification or add additional context in comments.

Comments

0

You are almost done.

With little modification's,

var array =[];

$('li').each(function () {
  array.push($(this).html());
});

Have a demo

Comments

0

Working Demo Here

You need to use push for adding values to your array

 items.push($(this).html());

See your code on JSFiddle

Comments

0

An alternative method is to use the Array prototype method slice, but since there's a jQuery method of the same name, probably not necessary. Some nice information to know tho.

var array = [].slice.call($('li')).map(function(el) {
  return $(el).html();
});

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.