1

I am currently trying to setup a table in HTML with 2 columns of information. The 3rd column has a button, and when pressed, it deletes that entry from the table.

What I am looking for is that the user cannot select an item below the first entry in the table. The code I have for the table right now is listed below.

HTML:

<table>
  <tr>
    <th>Firstname</th>
    <th>Issue</th> 
    <th>Select</th>
  </tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td> 
    <td>
            <input type="button" value="Delete" onclick="deleteRow(this)"/>

    </td>
  <tr>
    <td>Eve</td>
    <td>Jackson</td> 
    <td>
            <input type="button" value="Delete" onclick="deleteRow(this)"/>

    </td>
  </tr>
</table>

JavaScript:

function deleteRow(btn) {
  var row = btn.parentNode.parentNode;
  row.parentNode.removeChild(row);
} 
0

2 Answers 2

1

As per i understand your question you need to delete first row not a second row.

Please see below code if you want to do like this.

function deleteRow(btn) {
  var row = btn.parentNode.parentNode;
  if(row.rowIndex<=1)
    row.parentNode.removeChild(row);
} 
<table>
  <tr>
    <th>Firstname</th>
    <th>Issue</th> 
    <th>Select</th>
  </tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td> 
    <td>
            <input type="button" value="Delete" onclick="deleteRow(this)"/>

    </td>
  <tr>
    <td>Eve</td>
    <td>Jackson</td> 
    <td>
            <input type="button" value="Delete" onclick="deleteRow(this)"/>

    </td>
  </tr>
</table>

Sign up to request clarification or add additional context in comments.

Comments

1

you can try with this code

<html>
<head><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head>
<body>


<table id="tbUser">
  <tr>
    <th>Name</th>
    <th>Location</th>
    <th></th>
  </tr>
  <tr>
    <td>Amit</td>
    <td>Ghatkopar</td>
    <td><button class="btnDelete">Delete</button></td>
  </tr>
  <tr>
    <td>Vicky</td>
    <td>Powai</td>
    <td><button class="btnDelete">Delete</button></td>
  </tr>
  <tr>
    <td>Sunny</td>
    <td>Powai</td>
    <td><button class="btnDelete">Delete</button></td>
  </tr>
  <tr>
    <td>John</td>
    <td>NewYork</td>
    <td><button class="btnDelete">Delete</button></td>
  </tr>
</table> 

<script>
$(document).ready(function(){

 $("#tbUser").on('click','.btnDelete',function(){
       $(this).closest('tr').remove();
     });

});

</script>
</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.