Rubans
Rubans

Reputation: 4488

How to compare if an id is more than a value in jquery

Is it possible to use the more than operator in a jquery criteria For e.g say i want to get all rows where the rowindex is more than a certain value. Is it possible somehow or do i need create my own function once I have the result set from jquery?

Upvotes: 0

Views: 151

Answers (4)

Reigel Gallarde
Reigel Gallarde

Reputation: 65264

you may want:

.slice( start, [ end ] ); Reduce the set of matched elements to a subset specified by a range of indices.

start An integer indicating the 0-based position after which the elements are selected. If negative, it indicates an offset from the end of the set.
end An integer indicating the 0-based position before 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.

Upvotes: 0

Andy E
Andy E

Reputation: 344675

For the example mentioned in your question, you can just use the :gt() selector:

// Get all rows with a rowIndex greater than 5:
var rowsPlus5 = $('#mytable tr:gt(5)');

For other scenarios, you might need to use the .filter() method with a callback function.

Upvotes: 3

jAndy
jAndy

Reputation: 236092

var $filtered = $('table').find('tr').filter(function(){
     return (this.rowIndex > 5);
});

Works.

It does not work with $('table').children('tr'), I have no clue why not. Somebody please?

Upvotes: 1

HurnsMobile
HurnsMobile

Reputation: 4381

I would try something similar to the following:

$('tr.selector').each(function(){
    var rowId = $(this).index();
    if (rowId > 5){
        $(this).addClass('red');
    }
 });

Upvotes: 0

Related Questions