Paul Attuck
Paul Attuck

Reputation: 2269

How to use each from array?

I use this: http://datatables.net/release-datatables/examples/api/select_row.html

Here is function return selcted rows:

function fnGetSelected( oTableLocal )
{
    var aReturn = new Array();
    var aTrs = oTableLocal.fnGetNodes();

    for ( var i=0 ; i<aTrs.length ; i++ )
    {
        if ( $(aTrs[i]).hasClass('row_selected') )
        {
            aReturn.push( aTrs[i] );
        }
    }
    return aReturn;
}

i do:

var arr = fnGetSelected(oTable);

this return me:

[tr.gradeA, tr.gradeA, tr.gradeA]

this is:

<tr class="gradeA even row_selected">
<td class=" sorting_1"><span class="my_values" test="aaa">Gecko</span></td>
<td>Camino 1.5</td>
<td>OSX.3+</td>
<td class="center">1.8</td>
<td class="center">A</td>
</tr>
<tr class="gradeA odd row_selected">
<td class=" sorting_1"><span class="my_values" test="bbb">Gecko</span></td>
<td>Netscape 7.2</td>
<td>Win 95+ / Mac OS 8.6-9.2</td>
<td class="center">1.7</td>
<td class="center">A</td>
</tr>
<tr class="gradeA even">
<td class=" sorting_1"><span class="my_values" test="bbb">Gecko</span></td>
<td>Netscape Browser 8</td>
<td>Win 98SE+</td>
<td class="center">1.7</td>
<td class="center">A</td>
</tr>

I add for this own span.my_values with attribute test. Now i would like get all attribute test for this.

I try:

    arr.each(function(index) {
        console.log($(this).children());
    });

but this return error: arr.each is not a function

How can i make it?

Upvotes: 2

Views: 1102

Answers (5)

T.J. Crowder
T.J. Crowder

Reputation: 1074989

jQuery's generic array looping function is $.each, not a member of Array.prototype, so:

$.each(arr, function() {
    console.log($(this).children());
});

Now i would like get all attribute test for this.

If you'd like to get the test attribute of the span with class "my_values" inside the elements in arr, you can use $.map:

var testValues = $.map(arr, function() {
    return $(this).find('span.my_values[test]').attr('test');
});

...which gives you an array of the test values.


Side note 1: Since you're using jQuery, which is inherently set-based, rather than having your fnGetSelected function return an Array of matching elements, how 'bout having it return a jQuery object?

function fnGetSelected( oTableLocal )
{
    return $(oTableLocal.fnGetNodes()).filter('.row_selected');
}

That creates a jQuery object containing all of the returned nodes, then filters it to only the ones with the class "row_selected" (as your original did) and returns it.

Then you can use it like this:

var rows = fnGetSelected(oTableLocal);
rows.each(function() {
    console.log($(this).children());
});

var testValues = rows.map(function() {
    return $(this).find('span.my_values[test]').attr('test');
}).get();

Side note 2: "test" is an invalid attribute for span elements, although browsers allow it. Look at using data-* attributes instead, e.g., "data-test" rather than "test".

Upvotes: 3

James Jithin
James Jithin

Reputation: 10555

You can use arr as:

    $(arr).each(function(index) {
        console.log($(this).children());
    });

Upvotes: 1

Mathieu Mah&#233;
Mathieu Mah&#233;

Reputation: 2746

Have you try :

 $.each(arr, function(index) {
    console.log($(this).children());
});

or :

 $(arr).each(function(index) {
    console.log($(this).children());
});

Upvotes: 2

Beno&#238;t
Beno&#238;t

Reputation: 7427

arr is an array. You have to do

$.each(arr,function(index,value) {
  console.log(value);
})

you can transform arr to a jQuery object and use arr.each() by changing your function:

function fnGetSelected( oTableLocal )
{
    var aReturn = $([]);
    var aTrs = oTableLocal.fnGetNodes();

    for ( var i=0 ; i<aTrs.length ; i++ )
    {
        if ( $(aTrs[i]).hasClass('row_selected') )
        {
            aReturn.pushStack( aTrs[i] );
        }
    }
    return aReturn;
}

Upvotes: 1

Fr&#233;d&#233;ric Hamidi
Fr&#233;d&#233;ric Hamidi

Reputation: 263047

The each() method only applies to jQuery objects, not to arrays. $.each(), however, supports arrays, so you can use it instead:

$.each(arr, function() {
    console.log($(this).children());
});

Upvotes: 1

Related Questions