Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8948341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:57:39+00:00 2026-06-15T12:57:39+00:00

https://stackoverflow.com/a/1234337/1690081 shows that array.length = 0; will empty array but in my code it

  • 0

https://stackoverflow.com/a/1234337/1690081

shows that array.length = 0;will empty array but in my code it doesn’t

here’s an sample:

window.onload = draw;
window.onload = getFiles;
var namelist = [];
function draw(){
    // assing our canvas to element to a variable
    var canvas = document.getElementById("canvas1");
    // create html5 context object to enable draw methods
    var ctx = canvas.getContext('2d');

    var x = 10; // picture start cordinate
    var y = 10; // -------||---------
    var buffer = 10; // space between pictures
    for (i=0; i<namelist.length; i++){
        console.log(namelist[i])
        var image = document.createElement('img');
        image.src = namelist[i];
        canvas.appendChild(image);
        ctx.drawImage(image,x,y,50,50);
        x+=50+buffer;
    }
}
function getFiles(){
    namelist.length = 0;// empty name list
    var picturesFiles = document.getElementById('pictures')
    picturesFiles.addEventListener('change', function(event){
        var files = picturesFiles.files;

        for (i=0; i< files.length; i++){
            namelist.push(files[i].name);
            console.log(namelist)
        }
        draw();
    }, false);

}

after i call getFiles() second time. It doesn’t remove the previous list, just appends to it. any idea why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:57:41+00:00Added an answer on June 15, 2026 at 12:57 pm

    You should empty the array in the event handler, not getFiles which is only called once per pageload. It is actually doing nothing because the array is already empty when the page loads.

    picturesFiles.addEventListener('change', function(event){
        namelist.length = 0; //empty it here
        var files = picturesFiles.files;
    
        for (i=0; i< files.length; i++){
            namelist.push(files[i].name);
            console.log(namelist)
        }
        draw();
    }, false);
    

    Another problem is that you cannot just set .src to the name of a file. That would make the request to your server for the file.

    To really fix this, just push the file objects to the namelist:

    namelist.push(files[i]);
    

    Then as you process them in draw, create localized BLOB urls to show them:

    var file = namelist[i];
    var url = (window.URL || window.webkitURL).createObjectURL( file );
    image.src = url;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran across an oddity here: https://stackoverflow.com/a/11084038/636656 It appears that assigning to a name
In the comments here -- https://stackoverflow.com/a/9393138/8047 -- I discovered that BOOL has some unexpected
Using the code from this answer: https://stackoverflow.com/a/9744961/514773 I've noticed that any time I enter:
I found some code online (stackoverflow https://stackoverflow.com/a/5774234/150062 ) that does exactly what I need.
I have modified a little code from here: https://stackoverflow.com/a/13357269/1360074 Since there is known bug
https://stackoverflow.com/a/64983/468251 - Hello, I have question about this code, how made that working with
So I asked does https://stackoverflow.com/questions/11144014/does-mysql-update-add-a-row-if-it-doesnt-exist and I was told that I'm using columns incorrectly.
Referring to this question: https://stackoverflow.com/questions/2035449/why-is-oop-hard-for-me class Form { protected $inputs = array(); public function
I found this answer to this question https://stackoverflow.com/a/7244888/1473523 , but in my situation am
First look at this url: https://stackoverflow.com/questions/tagged/xoxoxo/ This directory does not exists but somehow stackoverflow

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.