Remove item_name and order_item_quantity numbered identifiers
The next numbered identifier to remove is used in calculating the price.
for (var no = 1; no <= count; no++) {
console.log({itemname: $('#item_name' + no).val()});
if ($.trim($('#item_name' + no).val()).length == 0) {
alert("Please Enter Item Name");
$('#item_name' + no).focus();
return false;
}
When looping through each item, it really should be each item row that is looped through. That way we can easily gain access to anything in each row that we need to access.
Before making much progress, we need to replace the return false statements. Those are all designed to prevent the default behaviour of submitting the form from taking place.
A much safer way to achieve that is by using the evt.preventDefault() method instead.
// $('#create_invoice').click(function() {
$('#create_invoice').click(function(evt) {
...
if ($.trim($('#item_name' + no).val()).length == 0) {
alert("Please Enter Item Name");
$('#item_name' + no).focus();
// return false;
evt.preventDefault();
return;
}
We can now turn that for loop into a forEach loop instead. The row index starts at 0, so Iāll have the no
value be one more than the row index.
// for (var no = 1; no <= count; no++) {
rows.each(function (index, itemRow) {
no = index + 1;
...
// }
});
That lets us keep on using the no
variable for now, as we convert the rest of the code to use itemRow instead.
Because preventDefault() is being used, we donāt need to use return anymore inside of the each loop.
rows.each(function (index, itemRow) {
...
evt.preventDefault();
// return;
}
With the console.log, we can make a simple request for the item name, and use that instead.
var itemName = $(itemRow).find("select");
// console.log({itemname: $('#item_name' + no).val()});
console.log({itemname: itemName.val()});
We can now replace all old occurrences of item_name with the updated itemName one instead.
// if ($.trim($('#item_name' + no).val()).length == 0) {
if ($.trim(itemName.val()).length == 0) {
...
// $('#item_name' + no).focus();
itemName.focus();
We can do the same thing with itemQuantity too.
var itemQuantity = $(itemRow).find("[name='order_item_quantity[]']");
// if ($.trim($('#order_item_quantity' + no).val()).length == 0) {
if ($.trim(itemQuantity.val()).length == 0) {
...
// $('#order_item_quantity' + no).focus();
itemQuantity.focus();
Now that things are working without the no
variable, we can remove that, and the numbered item_name and item_quantity identifiers too.
// html_code += '<td><select name="item_name[]" id="item_name' + count + '" class="form-control select3"><option value="">ŚÆŪŲ±Ų§Ł</option>' + options + '</select></td>';
html_code += '<td><select name="item_name[]" class="form-control select3"><option value="">ŚÆŪŲ±Ų§Ł</option>' + options + '</select></td>';
// html_code += '<td><input type="text" name="order_item_quantity[]" id="order_item_quantity' + count + '" data-srno="' + count + '" class="form-control input-sm number_only order_item_quantity" /></td>';
html_code += '<td><input type="text" name="order_item_quantity[]" data-srno="' + count + '" class="form-control input-sm number_only order_item_quantity" /></td>';
...
rows.each(function (index, itemRow) {
// no = index + 1;
I also removed the final submit line as it seems to achieve nothing.
// $('#invoice_form').submit();
That was in the click event handler of the submit button, so the event handler is going to submit anyway, so long as none of the preventDefault() parts of the code are executed.
The updated code with the above changes is found at https://jsfiddle.net/vLjdwktc/4/