Solution can use jQuery or be plain JavaScript.
I want to remove a table row after user has clicked the corresponding button contained in the table row cell so for example:
<script>
function SomeDeleteRowFunction() {
//no clue what to put here?
}
</script>
<table>
<tr>
<td><input type="button" value="Delete Row" onclick="SomeDeleteRowFunction()"></td>
</tr>
<tr>
<td><input type="button" value="Delete Row" onclick="SomeDeleteRowFunction()"></td>
</tr>
<tr>
<td><input type="button" value="Delete Row" onclick="SomeDeleteRowFunction()"></td>
</tr>
</table>
This question is related to
javascript
jquery
html-table
row
Following solution is working fine.
HTML:
<table>
<tr>
<td>
<input type="button" value="Delete Row" onclick="SomeDeleteRowFunction(this);">
</td>
</tr>
<tr>
<td>
<input type="button" value="Delete Row" onclick="SomeDeleteRowFunction(this);">
</td>
</tr>
<tr>
<td>
<input type="button" value="Delete Row" onclick="SomeDeleteRowFunction(this);">
</td>
</tr>
</table>
JQuery:
function SomeDeleteRowFunction(btndel) {
if (typeof(btndel) == "object") {
$(btndel).closest("tr").remove();
} else {
return false;
}
}
I have done bins on http://codebins.com/bin/4ldqpa9
Using pure Javascript:
Don't need to pass this
to the SomeDeleteRowFunction()
:
<td><input type="button" value="Delete Row" onclick="SomeDeleteRowFunction()"></td>
The onclick function:
function SomeDeleteRowFunction() {
// event.target will be the input element.
var td = event.target.parentNode;
var tr = td.parentNode; // the row to be removed
tr.parentNode.removeChild(tr);
}
As @gaurang171 mentioned, we can use .closest() which will return the first ancestor, or the closest to our delete button, and use .remove() to remove it.
This is how we can implement it using jQuery click event instead of using JavaScript onclick.
HTML:
<table id="myTable">
<tr>
<th width="30%" style="color:red;">ID</th>
<th width="25%" style="color:red;">Name</th>
<th width="25%" style="color:red;">Age</th>
<th width="1%"></th>
</tr>
<tr>
<td width="30%" style="color:red;">SSS-001</td>
<td width="25%" style="color:red;">Ben</td>
<td width="25%" style="color:red;">25</td>
<td><button type='button' class='btnDelete'>x</button></td>
</tr>
<tr>
<td width="30%" style="color:red;">SSS-002</td>
<td width="25%" style="color:red;">Anderson</td>
<td width="25%" style="color:red;">47</td>
<td><button type='button' class='btnDelete'>x</button></td>
</tr>
<tr>
<td width="30%" style="color:red;">SSS-003</td>
<td width="25%" style="color:red;">Rocky</td>
<td width="25%" style="color:red;">32</td>
<td><button type='button' class='btnDelete'>x</button></td>
</tr>
<tr>
<td width="30%" style="color:red;">SSS-004</td>
<td width="25%" style="color:red;">Lee</td>
<td width="25%" style="color:red;">15</td>
<td><button type='button' class='btnDelete'>x</button></td>
</tr>
jQuery
$(document).ready(function(){
$("#myTable").on('click','.btnDelete',function(){
$(this).closest('tr').remove();
});
});
Try in JSFiddle: click here.
you can do it like this:
<script>
function SomeDeleteRowFunction(o) {
//no clue what to put here?
var p=o.parentNode.parentNode;
p.parentNode.removeChild(p);
}
</script>
<table>
<tr>
<td><input type="button" value="Delete Row" onclick="SomeDeleteRowFunction(this)"></td>
</tr>
<tr>
<td><input type="button" value="Delete Row" onclick="SomeDeleteRowFunction(this)"></td>
</tr>
<tr>
<td><input type="button" value="Delete Row" onclick="SomeDeleteRowFunction(this)"></td>
</tr>
</table>
Source: Stackoverflow.com