I have a table in below format,
<table>
<tr>
<td>Dynamic Text1</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td>Dynamic Text2</td>
</tr>
<tr>
<td>Dynamic Text3</td>
</tr>
<tr>
<td> </td>
</tr>
</table>
I want to get the last td
containing something other than just a
. In the example markup shown this would be Dynamic Text3
, but I will not know the specific text in advance. This is simply possible by running in a loop but, is there any way to do this without using each
?
Update:
$('td').filter(function(){
return !!$.trim($(this).text());
}).last().css('color', 'red');
Demo: Fiddle
This should work now
var td = $('table td:last');
function findTD() {
if (!td.text().replace(/\u00a0/g, "").length) {
if (td.parent().is(':not(:first)')) {
td = td.parent().prev().find('td');
return findTD();
} else {
return 'No text found!';
}
} else {
return td.text();
}
}
alert(findTD());
FIDDLE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With