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

106
Views
javascript addEventListener will run more than once after several clicking

Click button will trigger setup function and add change event to input file, after choosing file , it will upload. The question is that it will run multiple times based on uploading times. I tried to reset the input file value after uploading , and still it will run twice in second times. How to fix this problem, I don't want to refresh page, and also I want it only run one time every click.

html code below

<input onclick='setup()' value='btn' type='button' class='btn btn-info'>
<form id='formid' method='POST' enctype='multipart/form-data'>
     <input accept='image/*' id='fileid' type='file' name='my_file' onsubmit='return false' />
     <input  type='submit' value='Submit' />
</form>

js code below

function setup(key) {
    document.getElementById('fileid').click();
    document.getElementById('fileid').addEventListener('change', submitForm);
    function submitForm() {
        var data = new FormData();
        data.append('my_file', $('#fileid').prop('files')[0]);
        $.ajax({
        url: 'ImageUpload.php',
        type: 'POST',               
        processData: false,
        contentType: false,
        data: data,
        success: function(data) {},
        error: function (error) {}
            });
        }
}
about 4 years ago · Juan Pablo Isaza
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!