Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete first row from two table in html using jquery

Tags:

jquery

i have two table in html with same summary value , the code is created server side and i have no access to generate tables with different ids or ...

<table summary='somefreakydummytext'></table>
<table summary='somefreakydummytext'></table>

i want to delete first row from both table , but following code only delete first row from first table

$("table[summary='somefreakydummytext'] tr:first").remove();

i tested

 $("table[summary='somefreakydummytext']) 

it is returning two table . so how can i delete first row from both table with jquery .

thanks folks.

like image 912
user6599525 Avatar asked Oct 29 '25 19:10

user6599525


1 Answers

The :first is a jQuery selector which selects first among the selected elements. So use :first-child pseudo-class selector to get the first child element from the each table.

$("table[summary='somefreakydummytext'] tr:first-child").remove();

$("table[summary='somefreakydummytext'] tr:first-child").remove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table summary='somefreakydummytext'>
  <tr>
    <td>1</td>
  </tr>
  <tr>
    <td>2</td>
  </tr>
  <tr>
    <td>3</td>
  </tr>
</table>
<table summary='somefreakydummytext'>
  <tr>
    <td>1</td>
  </tr>
  <tr>
    <td>2</td>
  </tr>
  <tr>
    <td>3</td>
  </tr>
</table>

To avoid nested table element use css direct child(>) selector.

$("table[summary='somefreakydummytext'] > tbody > tr:first-child").remove();

$("table[summary='somefreakydummytext'] > tbody > tr:first-child").remove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table summary='somefreakydummytext'>
  <tr>
    <td>1</td>
  </tr>
  <tr>
    <td>2</td>
  </tr>
  <tr>
    <td>3</td>
  </tr>
</table>
<table summary='somefreakydummytext'>
  <tr>
    <td>1</td>
  </tr>
  <tr>
    <td>2</td>
  </tr>
  <tr>
    <td>3</td>
  </tr>
</table>
like image 157
Pranav C Balan Avatar answered Oct 31 '25 13:10

Pranav C Balan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!