Skylight
Skylight

Reputation: 221

Tell Selector to return array inside array

$('.data').has(':checkbox:checked').find('.name, .street1, .street2, .county, .city, .postal, .country');

Returns 7 elements inside object in format of [name, street, street2, county, city, postal, country]

But in case if I have more than 1 set of these 7 elements on my page, it will return 14, 21, 28 and so on inside still one single object in a row without any separators.

Is there any way I can iterate through every 7 elements? Tell selector to return me object inside object with 7 elements? ie [[name, street, street2, county, city, postal, country], [name, street, street2, county, city, postal, country]] and so on?

Upvotes: 1

Views: 32

Answers (2)

Travis J
Travis J

Reputation: 82287

Make multiple data class elements

html

<div class="data">1-7</div>
<div class="data">1-7</div>
<div class="data">1-7</div>

js

var dataArrays = [];
$('.data').has(':checkbox:checked').each(function(){
 dataArrays.push($(this).find('.name, .street1, .street2, .county, .city, .postal, .country'));
});

Upvotes: 2

Jeremy Blalock
Jeremy Blalock

Reputation: 2619

Using the each method on .data, you could iterate through the items:

$('.data').has(':checkbox:checked').each(function() {
    var vals = $(this).find('.name, .street1, .street2, .county, .city, .postal, .country');
    some_function(vals);
});

You may have to restructure the way the result is used, but this is the more standard jQuery format.

Upvotes: 1

Related Questions