vtni
vtni

Reputation: 980

jQuery selector attribute range

I have a table with some rows and cols, each of them hast an attribute:

<tr attr-y="1"><td attr-x="1">...</td><td attr-x="2">...</td>...</tr>
<tr attr-y="2"><td attr-x="1">...</td><td attr-x="2">...</td>...</tr>
....

Now I want to edit some of these cells and tried to select them by a range, eg. (attr-y between 3 and 5, attr-x = 4)

$('#mytable tr[attr-y>3][attr-y<5] td[attr-x=4]')

But that gives me all cells in that column.

Can I select the cells direct (with selector statement)?

Upvotes: 2

Views: 1541

Answers (1)

Trav L
Trav L

Reputation: 15192

You can use .filter() to perform more complex query:

$('#mytable td').filter(function() {
    return ($(this).parent().attr('attr-y') > 3 && $(this).parent().attr('attr-y') < 5)
        && ($(this).attr('attr-x') == 4)
})
.css('background-color', 'aqua');

Example: http://jsfiddle.net/8wH4M/1/

Upvotes: 5

Related Questions