Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery array into eq function

Tags:

arrays

jquery

I want to send array into eq function as parameter. Like that:

$(this).find('tr').not(':eq(array)').each(function(){

});

I did this with using a loop and eval function but it doesn't look easy editable. This is my code.

$.fn.grilestir = function(options){

var nots = '';

for(var i=0;i<options.row_numbers.length;i++){
    nots += "not(':eq("+options.row_numbers[i]+")').";
}

    eval("$(this).find('tr')."+nots+"each(function(){\
        var tr = $(this); var orj;\
        if(options.mod == 'passive-rows'){\
            $(this).mouseover(function(){\
                orj = tr.css('backgroundColor');\
                tr.css('backgroundColor', '#777777');\
            });\
            $(this).mouseout(function(){\
                tr.css('backgroundColor', orj); \
            });\
        }\
    });");

}

Is there any way to do this?

like image 699
FURKAN ILGIN Avatar asked May 19 '26 20:05

FURKAN ILGIN


1 Answers

I am assuming that your array contains a set of numbers that represent element indexes. The eq selector will not work with that.

You could use filter to reduce the matched set of elements to those at indexes within your array:

var arr = [1, 2];
$("someSelector").filter(function(index) {
    return arr.indexOf(index) > -1;
});

Here's a working example.

Note the use of Array.prototype.indexOf, which is not available in older browsers (notable IE < version 9). However, there are plenty of shims available to solve that problem. Or, as noted in the comments (thanks @mcgrailm), you could use jQuery.inArray:

var arr = [1, 2];
$("someSelector").filter(function(index) {
    return $.inArray(index, arr) > -1;
});
like image 61
James Allardice Avatar answered May 21 '26 15:05

James Allardice



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!