views:

28

answers:

2

Hi,

I have an array filled with artificiality created option elements, the way I create them is the following:

var daysArr = new Array();
for(i=1; i<=31; i++){
    daysArr.push('<option value="'+ i +'">'+ i +'</option>');
}
$(daysArr.join(''));

What I'm trying to do is to use a selector on this array, like that:

$(daysArr.join('')).find('option:lt(5)');

The only thing I got is an empty array, even for .find('option'); There is the following info in jQ documentation for lt() selector:

Select all elements at an index less than index within the matched set.

Mine array is an index type array. I'll be glad if someone can tell me from where comes the problem.

+1  A: 
var daysArr = $('<select>');
for(i=1; i<=31; i++){
   daysArr.append('<option value="'+ i +'">'+ i +'</option>');
}

daysArr.children('option:gt(5)')
jAndy
he's wrapping it in `$` tho
David Hedlund
Actually, he's wrapping the string resulting from joining all option texts, so he'll get jQuery methods: $(daysArr.join(''))
dguaraglia
jAndy your answer is useful, but it's not what I'm looking for because I'm trying to get the options without creating an additional selector :)
bozhidarc
+2  A: 

daysArr is your array of options. Trying to find() something in them will go down one level too deep.

It would work if you were to do something like this:

$('<select>' + daysArr.join('') + '</select>').find('option:lt(5)');

But of course a simpler way of achieving the same would be

daysArr.slice(0,5);
David Hedlund
I'm again doing the things in the hardest way.thanks a lot :)
bozhidarc