Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

224
Views
How do I get this alert to stop looping?

I have this Javascript code where I want the users submission to only be pushed to the database, when all of the values are filled. The only problem is, a user can input a new row of data and that needs to be checked with a loop. Every time I go to click submit, the data is pushed regardless if the fields are filled or not. And it loops each time. I've tried moving data.push(assetInfo); outside the brackets and it still prints submission accepted how ever many times the user clicks submit. Any help would be appreciated!

$('#submit').click(() =>{
        $('form').on('submit', function(e){
            e.preventDefault();

    
            //Format all Data into JSON for Submission
            let data = [];

            // Iterate over all rows and store data
            for (let i = 1; i <= count; i++){

                // Skip Row if it was Removed
                if (!$(`tr[index=${i}]`).length) continue;

                // Store all Info from this row
                let assetInfo = {

                    asset_tag_no: $(`#asset_tag_no${i}`).val(),
                    manufacturer: $(`#manufacturer_serial_no${i}`).val(),
                    descriptions: $(`#description${i}`).val(),
                    costs: $(`#cost${i}`).val(),
                    po_no: $(`#po_no${i}`).val(),
                    remarks: $(`#remarks${i}`).val(),
                }
        
                if (assetInfo.asset_tag_no && assetInfo.manufacturer && assetInfo.descriptions && assetInfo.costs && assetInfo.po_no && assetInfo.remarks  != ''){ 
                    alert('Submission Accepted');
                }
                // Add Info to array
                data.push(assetInfo);
            }
    
        
            // Upload JSON to Database
            console.log(data);
        });
    });
  })
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You don't need to move the push outside the brackets. You need to move it inside the brackets:

            if (assetInfo.asset_tag_no && assetInfo.manufacturer && assetInfo.descriptions && assetInfo.costs && assetInfo.po_no && assetInfo.remarks  != ''){ 
                alert('Submission Accepted');
                // Add Info to array
                data.push(assetInfo);
            }

EDIT

If you want to push a row exactly once, then you can replace

if (!$(`tr[index=${i}]`).length) continue;

with

    let currentRow = $(`tr[index=${i}]`);
    if ((!currentRow.length) || (currentRow.hasClass("submitted"))) continue;
    currentRow.addClass("submitted");
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!