ShwetankJoshi
Coder
Thank you! for this great content. I am making a small project of creating a invoice maker website using JS, for adding new items in invoice and deleting it i am making function with several refrence vdo's i managed to create addrow function but not able to create delete row function.also I am new to JS. on creating remove_tr function i am getting the error in the console that
"script.js:34 Uncaught TypeError: This.closest is not a function
at remove_tr (script.js:34:14)
at HTMLButtonElement.onclick (invoice.html:97:80)"
plz help me in this. It will be great for me, Thanks.
the code section is given below for your understanding.
HTML code section =>
JS code section=>
"script.js:34 Uncaught TypeError: This.closest is not a function
at remove_tr (script.js:34:14)
at HTMLButtonElement.onclick (invoice.html:97:80)"
plz help me in this. It will be great for me, Thanks.
the code section is given below for your understanding.
HTML code section =>
HTML:
<tbody id="table_body">
<tr class="item-row">
<td class="item-name">
<div class="Item"><input type="text" name="" placeholder="Enter Item..."><br>
</div>
</td>
<td class="description">
<textarea id=" description " type="text " name=" " placeholder="Enter Description... "></textarea></td>
<td class="cost "><input type="number" name=" " placeholder="Enter Cost... "></td>
<td class="qty "><input type="number" name=" " placeholder="Enter Quantity... "></td>
<td><span class="price ">650.00</span></td>
<td>
<div class="action_container">
<button class="danger" onclick="remove_tr('This')">
<i class="fa fa-close"></i>
</button>
</div>
</td>
JS code section=>
JavaScript:
function create_tr(table_id) {
let table_body = document.getElementById(table_id);
let first_tr = table_body.firstElementChild;
let tr_clone = first_tr.cloneNode(true);
let ref_Node = document.getElementById("ref_Node");
table_body.insertBefore(tr_clone, ref_Node);
}
function remove_tr(This) {
if (This.closest("tbody").childElementCount == 1) {
alert("You Don't have Permission to Delete This ?");
} else {
This.closest("tr").remove();
}
}