Tim Sheiner Tim Sheiner - 29 days ago 5
jQuery Question

Finding text in a table with jQuery

I have a table that has rows like this:

<tr id="" class="objectRow">
<td class="bulkSelector"><input id="" type="checkbox" value=""/></td>
<td class="favorite"></td>
<td class="name"><a id="" class="" href="">Ut Urna Nisl</a></td>
<td class="description"><p>Nam feugiat tincidunt massa nec venenatis. Mauris egestas consectetur magna</p></td>
<td class="modifiedDate"><p>5/20/2009</p></td>

I want to create a jQuery wrapped set of all the text elements that I can then send to function that will truncate them if they don't fit in their cells.

I can't figure out how to get the wrapped set.

Was trying this, but it doesn't work:

var textNodes = $('#resultsTable .objectRow')
.filter(function(){ return this.nodeType == 3; })
.filter(function(){return this.nodeValue != null});


jQuery's text function returns the combined text contents of the element, so you don't have to worry about nodeTypes and such. So you could then just filter all elements whose text contents is blank:

$('tr.objectRow', '#resultsTable').find('td').filter(function() {
    return $.trim($(this).text()) != '';

That's going to end up giving you all the <td>s in the row that have any text in it, and you could do what you want to do by again getting the text() value of the table cell.

Regarding your comment, this should do it:

$('tr.objectRow', '#resultsTable').find('*').contents().filter(function() {
    return $.trim($(this).text()) != '';