1

I have an array with HTML elements from a selector. Looks like this:

var selection = $('.features_items');
// selection = [div.features_item, div.features_item];

The div looks like this:

<div class="features_item selected" value="2">

Now I want to get all values from the div's in the array: [2,3]

1
  • 1
    Technically, selection isn't an array -- it's an array-like jQuery object. This is only significant if you try to use methods like shift or pop, which won't work on jQuery objects. Commented Oct 24, 2011 at 13:40

2 Answers 2

3

You can use jQuery.map to map the jQuery object to an array in a single step:

var values = $.map(selection, function() {
    return $(this).val();
}); // values is now an array equal in length to 'selection'
Sign up to request clarification or add additional context in comments.

Comments

1

Do you want something like this?

var values = [];
$('.features_items').each(function()
{
  values.push( this.getAttribute('value') );
});
alert(values);

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.