I have a dynamic HTML tbody which I am creating on change event of a select option, so in my table, I have 3 input fields
ItemNamewhich isautocompleteI am using jquery UI for that, So when the user selects any itemName I am populating that ItemName's data to corresponding fields of a table, all data I have is in JSON format- then a user goes to next input field i.e
UnitQtyand enters something and on focus out of that I am doing some calculation - Then the last input field is
Disc%here I am doing 2-3 things, when a user enters some input and pressenterI am creating new row same as above with same functionality and some calculations are done and showed in a footer row I have named asTotal - When user want to create new row then only he/she will press enter otherwise user press tab and focus out from the
Disc%and same calculation I am doing
What my issue is
When user enters some data inside
Disc%and presstabI am calling a function to calculate the data, suppose some times user by mistake presstaband want to go back toDisc%and create new row, so when I go back toDisc%by pressingright Shift+tabit doses some calculation as the functioncalcDiscountruns again, sometimes if I goo back to edit theDisc%field then also it runs the functioncalcDiscountAlso sometimes used by mistake put leave some field empty in table's input field I have tried to alert whenever the user leaves any empty field, but that too is not helping out
Please check out fiddle I am doing like below
$(document).keypress(function (event) {
const row = event.target.parentElement.parentElement
var keycode = event.keyCode || event.which;
if (keycode == '13') {
if (event.target.matches('[name=discPercentagetd]')) {
calcDiscount(event.target.parentElement.parentElement)
if ($(row).parent().find('tr').length - $(row).index() === 1) {
rowappend(event.target.parentElement.parentElement.parentElement)
}
}
}
});
document.addEventListener("keydown", function (event) {
const row = event.target.parentElement.parentElement
if (event.target.matches('[name=discPercentagetd]')) {
var keycode = event.keyCode || event.which;
if (keycode == '9') {
calcDiscount(event.target.parentElement.parentElement)
}
}
});
Please check out my fiddle I have commented all the lines there
Note-: after entering DIsc% please go back to Disc% after focusing out using rightShift+tab and see the unitQty in footer it auto increases