views:

235

answers:

4

Hi

I have a table that has some rows in it.

Here is an example of my table just with alot less table rows.

<table>
 <tr>
   <td>bob</td>
   <td class="CheckThis">0 </td>
 </tr>
 <tr>
   <td>Jim</td>
   <td class="CheckThis">3</td>
 </tr>
</table>

Now I want to look at the second table cells and get rows back that have value greater then 0.

So in my above example it would get the entire row that contains "3" since it is greater then zero.

I don't see any sectors that do greater then something in jquery but I could have just missed it.

thanks

A: 

Sure, gt should do it. Check out the docs at Selectors/gt

Edit: Oh, you're talking about the value in the HTML of the element? You'd probably have to set up a .each loop with a filter or something. I'll check on this, sorry.

Alex JL
This selects by index, not by content.
BalusC
Yep, just noticed. I'd delete my post if that was possible.
Alex JL
+4  A: 

You can use the filter() function for this.

E.g.

var tds = $('td.CheckThis').filter(function() {
    return parseInt($(this).text()) > 0;
});

Update: after rereading the question once more, you actually want to select the tr elements containing the td's in question and thus not only the td's. In this case, please checkout Gumbo's answer for another code example which does that right.

BalusC
It's always advisable to include the number base for parseInt to avoid accidentally going into octal mode: `parseInt($(this).text(),10)`
slebetman
Only if the numbers starts with `0`, which I don't see in his example.
BalusC
But this doesn’t select the rows.
Gumbo
Yes, I realized that after reading your answer :)
BalusC
@BalusC: Well, you can rescue your answer by applying `parent` to get the parent afterwards: `var trs = tds.parent();`
Gumbo
+2  A: 

Try this:

$("tr").filter(function() {
    return parseInt($(this).children("td.CheckThis").text(), 10) > 0;
})

This will select you each TR element that has a TD child element with the class CheckThis that’s content is greater than 0.

Gumbo
Why .text()? I don't think I ever used this selector before. I usually use val() or html(). What is the difference? Is text just like the actual text and not the html tags?
chobo2
The `val()` works on **input** elements only: http://docs.jquery.com/Attributes/val The `html()` returns/sets html, not text. The `text()` escapes HTML. You don't need `html()` here as there's no means of HTML content.
BalusC
@chobo2: `val` is for input elements and `html` returns the HTML contents (markup) while `text` just returns the text contents. Please take a look at the jQuery documentation for details: http://docs.jquery.com/
Gumbo
A: 
var list = [];
$("table").find("tr td:eq(1):not(:contains('0'))").each(function(i,val){
    list.push($(this).parent()); //add TR
});
andres descalzo