Expected result is to get red all special divs between first and last selected div.
This works but couldnt be it better written?:
var s = $('div.selected'),
$spec = $('.speial'),
i = s.first().index('.speial'),
last = s.last().index('.speial');
for(;i<last;i++){
$spec.eq(i).addClass('gored');
}
I was trying this:
var s = $('div.selected'),
$spec = $('.speial'),
i = s.first().index($spec),
last = s.last().index($spec);
for(;i<last;i++){
$spec.eq(i).addClass('gored');
}
But it doesnt work… I am interested in the reason why.
Or maybe is there even better way to achieve the same result?
EDIT: I have made the fiddle to get picture of the problem 🙂 fiddle
jQuery API doc:
So It seems I have firstly misunderstood the relativity in both cases, so I thought that “elements matched by the selector” could be replaced by selection (jQuery object).
The answer is that .index() works another way than I expected.