GkDaGk
GkDaGk

Reputation: 55

How to apply AND operator in jQuery

I want to hide the rows from row index from 1 to 3. The below code is not working:

$("table").find("tr:gt(1)").find("tr:lt(3)").each(function () { $(this).hide() });

Upvotes: 0

Views: 1377

Answers (4)

Stefan
Stefan

Reputation: 5662

The first find() returns the matched elements and then the second one filters that selection further.

Try this selector

$('table tr:gt(0):lt(4)').hide();

Also, slice() would be a great option (if not the best)

$('table tr').slice(1,4).hide();

Upvotes: 2

Merlyn Morgan-Graham
Merlyn Morgan-Graham

Reputation: 59151

Try using the slice method.

From the docs:

Description: Reduce the set of matched elements to a subset specified by a range of indices

.slice( start [, end] )

start An integer indicating the 0-based position at which the elements begin to be selected. If negative, it indicates an offset from the end of the set.

end An integer indicating the 0-based position at which the elements stop being selected. If negative, it indicates an offset from the end of the set. If omitted, the range continues until the end of the set.

Code:

http://jsfiddle.net/tkVut/

$("table tr").slice(1, 3 + 1).hide();

Also note that the hide method doesn't require you to use each:

Hide the matched elements

Upvotes: 2

zzzzBov
zzzzBov

Reputation: 179284

A selector like this?

$('table tr:gt(0):lt(4)').hide();

I can't tell if you inclusively want indices 1 and 3, so for now I will assume you do.

If you didn't you could use:

$('table tr:gt(1):lt(3)').hide();

But that would simply be index 2:

$('table tr:eq(2)').hide();

If you need to keep $('table') as the base selector, you can use the find method:

$('table').find('tr:gt(#):lt(#)').hide().end().chain().more().stuff()

Upvotes: 1

James Allardice
James Allardice

Reputation: 166071

You could use filter to reduce the matched set of elements to those within the correct range:

$("table tr").filter(function(i) {
    return i > 1 && i < 3;
}).hide();

Note that I haven't used each to iterate over the set of matched elements at the end, because hide, like the majority of jQuery methods, applies to all elements in the set anyway.

Upvotes: 2

Related Questions