Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How select elements between interval

I have html and want to select interval 5 elements from 10'th position. How to do this?

My html:

<div class="chaire">
   <img alt="" src="2.gif">

</div>
<div class="chaire">
   <img alt="" src="2.gif">
</div>
<div class="chaire">
   <img alt="" src="2.gif">
</div>
<div class="chaire">
   <img alt="" src="2.gif">
</div>
...
<div class="chaire">
   <img alt="" src="2.gif">
</div>

I try with jquery:

$(".chaire:gt(10):lt(15)");

but it select me div and img tags. But I need div tags.

like image 845
Liutas Avatar asked Jan 14 '11 14:01

Liutas


2 Answers

Your code does the job just fine, and selects only <div> elements. Take a look at this example fiddle, which finds 5 <div> elements after the 10th (:gt(9):lt(15)).


The selector you have will only select elements with that class name, in the case of your example HTML, it's just the <div> elements. No <img> elements will be selected. As rcravens had already pointed out, the two selectors modify the result separately, so you need to use :lt() first - :lt(15):gt(9).

Updated example at: http://jsfiddle.net/teQkf/3/. The next part of the example code finds the <img> elements within the result and changes their src to something else.

You're better off using slice, which is only a single operation on the result and therefore less confusing, not to mention faster:

$(".chaire").slice(10,15);

(example)

like image 86
Andy E Avatar answered Oct 04 '22 21:10

Andy E


try this:

$(".chaire:gt(10):lt(5)");

Here is a jFiddle to play around with.

http://jsfiddle.net/rcravens/m3j6K/

It looks like chaining the 'gt' and 'lt' selectors means 'lt' is applied to what remains after the 'gt'.

Bob

like image 41
rcravens Avatar answered Oct 04 '22 21:10

rcravens