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 9243679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:51:34+00:00 2026-06-18T08:51:34+00:00

This is my code: var rowData = []; var showFiles = function () {

  • 0

This is my code:

        var rowData = [];

        var showFiles = function () {
            var input = document.getElementById('doc-upload');
            if (input.files.length > 0) {
                for (var i = 0; i < input.files.length; i += 1) {
                    rowData.push([input.files[i].name]);
                };
                console.log(rowData);
            };
        };
        document.getElementById('doc-upload').addEventListener('change', showFiles, this);

‘rowData’ recives a normal value for the first time i upload some thing.
At the second try it starts to duplicate incoming values.

If i uploaded 1 file first try it’s ok.
Second time i wold get 2 same files.
Third 4 files, exetera.

Why this is happening?

UPD:

More explanations:
What happaning is: i upload x.doc my array is [x.doc], now i want to upload y.doc. I choose y.doc and my array turns in to [x.doc, y.doc, y.doc], then i upload z.doc and aaray looks like [x.doc, y.doc, y.doc, z.doc, z.doc, z.doc, z.doc]. And so on. It have to do some thing with length property, i messed it up some how.

UPD2:

Now the full version of the code:

    //file upload
    var rowData = [];
    docAddButton.addListener('click', function () {
        var showFiles = function () {
            var input = document.getElementById('doc-upload');
            if (input.files.length > 0) {
                for (var i = 0; i < input.files.length; i += 1) {
                    if (rowData.indexOf(true, input.files[i].name, Math.round(input.files[i].size * 0.001) + ' кб') === -1) {
                        rowData.push([
                            true,
                            input.files[i].name, 
                            Math.round(input.files[i].size * 0.001) + ' кб'
                        ]);
                    }
                }
                console.log(rowData);
                tableModel.setData(rowData);
            };
        };
        document.getElementById('doc-upload').addEventListener('change', showFiles, this);
    });

Thanx every one who helped!

Solution:
You can see from UPD2 that I have my function inside eventListener ‘click’. What was happening is every time I pressed my input button it was reciving extra ‘change’ listener.

I changed .addListener for input to .addListenerOnce.

  • 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-18T08:51:35+00:00Added an answer on June 18, 2026 at 8:51 am

    Presumably you’re not reloading the page, and you’re not clearing out rowData. You haven’t shown how/where you define rowData, but it would appear that the same instance of it is reused between calls to showFiles. (If you haven’t declared it anywhere, you’re falling prey to The Horror of Implicit Globals.)


    From your comments below, it sounds like you want to keep the entries in rowData, but you want to only add new entries. If so, you’ll need an explicit check:

    var showFiles = function () {
        var input, i, file;
    
        input = document.getElementById('doc-upload');
        for (i = 0; i < input.files.length; i += 1) {
            file = input.files[i].name;
            if (indexOfFile(rowData, file) === -1) {
                rowData.push([file]);
            }
        };
        console.log(rowData);
    };
    
    function indexOfFile(data, file) {
        var index;
    
        for (index = 0; index < data.length; ++index) {
            // Note that the 0 may need to be changed if the
            // `rowData.push([file])` line above doesn't put
            // the filename at index 0
            if (data[index][0] === file) {
                return index;
            }
        }
        return -1;
    }
    

    Side note: You’ve said that the reason you’re pushing an array rather than just the filename is that there’s other information you’re also including in that array but you left it out of your question. That’s fine, but it may be delicate, see my note about index 0 above. You might consider using an array of objects instead:

    rowData.push({
        file: file,
        other: "info",
        goes: "here"
    });
    

    …and then in indexOfFile:

    if (data[index].file === file) {
    

    That way, the code is less susceptible to breaking if you change the structure of what you’re pushing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using this code var html='<div class=somediv></div>'; var target=document.getElementById('contentArea'); target.appendChild(html); I'm getting Uncaught Error: NOT_FOUND_ERR:
I have made this code: var foo=document.createElement(div); var childs=foo.getElementsByTagName(*); console.log(childs.length);//0 OK var a=document.createElement(a); foo.appendChild(a);
I have the following code: var wait = 500; $(document).on(click,.new_game_list_row,function(event){ var self = $(this);
Consider this code: var img = new Image(); img.onload = function() { console.log(this.width); };
I have this code: var obj = function (i) { this.a = i; this.init
I have this code: var newProduct = new Product(); newProduct.Name = product name; newProduct.Description
Consider this javascript code: var bar = function () { alert(A); } var foo
I have this code: var deEdit = $(#opsEdit a, h1); deEdit.live(click, function(){ $.ajax({ url:
Take this code: var obj = { init: function(){ console.log(obj.count); //or console.log(this.count); }, count:
This code var offset = $('#snapwrap_inner').offset(); alert(offset.left) in FF returns 0px but in Opera

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.