Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a row based on its index?

I want to delete a selected row using jQuery. First, while clicking add button, rows are added dynamically with a delete button. Now I want to delete the selected row.

I am using the following code to add rows dynamically:

$("#btnAdd").on('click', function (){  
  $('#Time tr').last().after('<tr><td><span style="font-size:14px;">Diplamo/Certificate :</span><input type="textbox" input id="addedValue12" name = "Certificate"></td><td><span style="font-size:14px;">Percentage :</span><input type="textbox" id="addedValue123" name = "CertificatePer"></td></tr>'); 		
});
<input id="btnAdd" type="button" value="add" />
<table id="Time"> 
    <tr>
    </tr>
</table>

I am displaying values in a JSP page.

<c:forEach var="var1" items="${empedu.empCertificate}">
  Certificate Name:<input value="${var1.certification}" name="Certificate" type="text" title="Only Text Allowed" pattern="[a-zA-Z\s'.,@:&?!()$#/\\]+" />
  Percentage: <input value="${var1.percentage}" name="CertificatePer" style="width: 100px;" type="text" max="100" />
  <input type="button" id="deleteRow" name="delete" value="delete" /><br/>
</c:forEach>

It is displayed on screen like this:

Screenshot

Here, If I click the third row, I want to delete the third row. How can I do this?

like image 307
sathish kumar Avatar asked Jan 17 '26 20:01

sathish kumar


1 Answers

$("#Time").on("click", ".delete", function(){

    $(this).closest("tr").remove();

});

Assuming delete is the class of the delete button. I have used .on as the rows and thus delete buttons are dynamically added.

This is picking the closest tr from the clicked delete button, and removing it.

Working Fiddle

like image 54
void Avatar answered Jan 20 '26 10:01

void