ConorReidd ConorReidd - 2 months ago 6
Javascript Question

Removing <tr> from table without :contains()

Okay,

This may sound easier than it is, I need to use either Javascript or jQuery to remove a

<tr>
from a table, I don't know what row this
<tr>
will be, nor do i know which
<tr>
I will be removing and the only way I can find out which
<tr>
to remove is from the text inside the child's
<td>
.

I cannot use the :contains() selector from jQuery as I'm using ajax to get the elements and it just seems broken.

$("td:contains(var)").parent().remove();


just gives me a DOMexception error.

Is there a way I can loop through all the
<td>
's in a table and check the text contents of them?

Answer

This will check the text of each one:

$('tr').each(function() {
  var $tds = $(this).find('td');
  $tds.each(function() {
    if ($(this).text() === "some text") {
      $(this).parent().remove()
    }
  });     
})
Comments