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?
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;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With