views:

171

answers:

1

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>
</tr>

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')
.contents()
.filter(function(){ return this.nodeType == 3; })
.filter(function(){return this.nodeValue != null});

Thanks!

+2  A: 

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()) != '';
});
Paolo Bergantino
Thanks. I didn't ask the question right.The set I need is the elements that contain text. In my example this would consist of one a and 2 p nodes.
Tim Sheiner