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

188
Views
Execute function on onloadend event

I'm trying to execute functions after an onloadend event in order to append the uploaded image to a canvas and do some post procesing.

let fileInput = document.getElementById('fileinput');
    fileInput.addEventListener('change', function(ev) {
    if(ev.target.files) {
        let file = ev.target.files[0];
        var reader  = new FileReader();
        reader.readAsDataURL(file);
        reader.onloadend = function (e) {
            var image = new Image();
            image.src = e.target.result;
            image.onload = function(ev) {
                function one(){
                    // some code here...
                }

                function two(){
                    // some code here...
                }

                // etc..
            }
        }
    }
});

How can i do that ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your code works. You are successfully running the code in the onloadend event, but your code merely declares two functions. It doesn't run them. I have included some console.logs to illustrate my point.

let fileInput = document.getElementById('fileinput');
    fileInput.addEventListener('change', function(ev) {
    
    console.log('change event fired');  
  
    if(ev.target.files) {
        let file = ev.target.files[0];
        var reader  = new FileReader();
        reader.readAsDataURL(file);
        reader.onloadend = function (e) {
        
            console.log('loadEnd event fired');
        
            var image = new Image();

            function one() {
              console.log('image.load event fired');
              const canvas = document.getElementById("canvas");
              const context = canvas.getContext("2d");
              context.drawImage(image,0,0);
            };

            image.onload = one;
            image.src = e.target.result;
        }
    }
});
<form>
    <input type=file id=fileinput />
</form>

<canvas id=canvas width=300 height=300></canvas>

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!