Jasir alwafaa
Jasir alwafaa

Reputation: 586

Remove last td from table jquery

I am using a table and dynamically creating a column (td) on click jquery. I want to remove the column as the last td, but in each td one more table is coming, hence I can't remove the entire td.

<table width="100%" border="0">
  <tr>
    <td class="myclass">
        <table width="100%" border="0">
          <tr>
            <td>&nbsp;</td>
          </tr>
        </table>
    </td>
    <td class="myclass">
        <table width="100%" border="0">
          <tr>
            <td>&nbsp;</td>
          </tr>
        </table>
    </td>
   
  </tr>
</table>

I want to remove the last td having the class name my class. I tried like this:

$('tr').find('td:last').remove();

But it's removing the last td from the inner table, I want to remove the entire last td having class myclass.

Upvotes: 0

Views: 1811

Answers (3)

nartoan
nartoan

Reputation: 378

Try :

$('tr').find('td.myclass:last').remove();

Upvotes: 2

Pranav C Balan
Pranav C Balan

Reputation: 115272

Get td with the class

$('tr > td.myClass:last-child').remove();

or if you want t remove the last one from the collection

$('td.myClass:last').remove();

Upvotes: 0

Jyothi Babu Araja
Jyothi Babu Araja

Reputation: 10292

You can select specific td with .myclass

$("td.myclass:last").remove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table width="100%" border="0">
  <tr>
    <td class="myclass">
        <table width="100%" border="0">
          <tr>
            <td>&nbsp;First</td>
          </tr>
        </table>
    </td>
    <td class="myclass">
        <table width="100%" border="0">
          <tr>
            <td>&nbsp;Last</td>
          </tr>
        </table>
    </td>

  </tr>
</table>

Upvotes: 0

Related Questions