Access for loop data inside eventlistener [duplicate]





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0
















This question already has an answer here:




  • JavaScript closure inside loops – simple practical example

    42 answers




I've been trying to create a snippet in order to upload multiple files. The code below works but I need to get my image id in order to clear my filesCollection variable when the user removes the image.



Since the event listener load all files altogether, the variable file.name always show the same value for my dynamically created images.



Any insight that could help me?



Thanks in advance



    for (var i = 0; i < files.length; i++) {
var file = files[i];
var picReader = new FileReader();

picReader.addEventListener("load", function (event) {
var picFile = event.target;
var div = document.getElementById("uploadboard");

div.innerHTML = div.innerHTML + "<span class='preview'>
<img id='"+ file.name +"' src='"+ picFile.result +" '/>
<button class='remover'></button></span>";
}

picReader.readAsDataURL(file);
filesCollection[file.name] = file;

}









share|improve this question













marked as duplicate by charlietfl javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 2:20


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

























    0
















    This question already has an answer here:




    • JavaScript closure inside loops – simple practical example

      42 answers




    I've been trying to create a snippet in order to upload multiple files. The code below works but I need to get my image id in order to clear my filesCollection variable when the user removes the image.



    Since the event listener load all files altogether, the variable file.name always show the same value for my dynamically created images.



    Any insight that could help me?



    Thanks in advance



        for (var i = 0; i < files.length; i++) {
    var file = files[i];
    var picReader = new FileReader();

    picReader.addEventListener("load", function (event) {
    var picFile = event.target;
    var div = document.getElementById("uploadboard");

    div.innerHTML = div.innerHTML + "<span class='preview'>
    <img id='"+ file.name +"' src='"+ picFile.result +" '/>
    <button class='remover'></button></span>";
    }

    picReader.readAsDataURL(file);
    filesCollection[file.name] = file;

    }









    share|improve this question













    marked as duplicate by charlietfl javascript
    Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 23 '18 at 2:20


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      0












      0








      0









      This question already has an answer here:




      • JavaScript closure inside loops – simple practical example

        42 answers




      I've been trying to create a snippet in order to upload multiple files. The code below works but I need to get my image id in order to clear my filesCollection variable when the user removes the image.



      Since the event listener load all files altogether, the variable file.name always show the same value for my dynamically created images.



      Any insight that could help me?



      Thanks in advance



          for (var i = 0; i < files.length; i++) {
      var file = files[i];
      var picReader = new FileReader();

      picReader.addEventListener("load", function (event) {
      var picFile = event.target;
      var div = document.getElementById("uploadboard");

      div.innerHTML = div.innerHTML + "<span class='preview'>
      <img id='"+ file.name +"' src='"+ picFile.result +" '/>
      <button class='remover'></button></span>";
      }

      picReader.readAsDataURL(file);
      filesCollection[file.name] = file;

      }









      share|improve this question















      This question already has an answer here:




      • JavaScript closure inside loops – simple practical example

        42 answers




      I've been trying to create a snippet in order to upload multiple files. The code below works but I need to get my image id in order to clear my filesCollection variable when the user removes the image.



      Since the event listener load all files altogether, the variable file.name always show the same value for my dynamically created images.



      Any insight that could help me?



      Thanks in advance



          for (var i = 0; i < files.length; i++) {
      var file = files[i];
      var picReader = new FileReader();

      picReader.addEventListener("load", function (event) {
      var picFile = event.target;
      var div = document.getElementById("uploadboard");

      div.innerHTML = div.innerHTML + "<span class='preview'>
      <img id='"+ file.name +"' src='"+ picFile.result +" '/>
      <button class='remover'></button></span>";
      }

      picReader.readAsDataURL(file);
      filesCollection[file.name] = file;

      }




      This question already has an answer here:




      • JavaScript closure inside loops – simple practical example

        42 answers








      javascript jquery ajax






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 2:09









      Hugo Di Mark DhersHugo Di Mark Dhers

      66




      66




      marked as duplicate by charlietfl javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 23 '18 at 2:20


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by charlietfl javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 23 '18 at 2:20


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes


















          0














          Because of the statement var




          The scope of a variable declared with var is its current execution context, which is either the enclosing function or, for variables declared outside any function, global.




          You can create a function using an approach with IIFE (Immediately Invoked Function Expression)



          for (var i = 0; i < files.length; i++) {
          var f = files[i];
          var picReader = new FileReader();

          picReader.addEventListener("load", (function (file) {
          return function(event) {
          var picFile = event.target;
          var div = document.getElementById("uploadboard");

          div.innerHTML = div.innerHTML + "<span class='preview'>
          <img id='"+ file.name +"' src='"+ picFile.result +" '/>
          <button class='remover'></button></span>";
          }
          )(f);
          }

          picReader.readAsDataURL(f);
          filesCollection[f.name] = f;
          }


          One more alternative is using the statement let




          The let statement declares a block scope local variable, optionally initializing it to a value.




          for (let i = 0; i < files.length; i++) {
          let file = files[i];
          let picReader = new FileReader();

          picReader.addEventListener("load", function (event) {
          let picFile = event.target;
          let div = document.getElementById("uploadboard");

          div.innerHTML = div.innerHTML + "<span class='preview'>
          <img id='"+ file.name +"' src='"+ picFile.result +" '/>
          <button class='remover'></button></span>";
          }

          picReader.readAsDataURL(file);
          filesCollection[f.name] = file;
          }





          share|improve this answer






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Because of the statement var




            The scope of a variable declared with var is its current execution context, which is either the enclosing function or, for variables declared outside any function, global.




            You can create a function using an approach with IIFE (Immediately Invoked Function Expression)



            for (var i = 0; i < files.length; i++) {
            var f = files[i];
            var picReader = new FileReader();

            picReader.addEventListener("load", (function (file) {
            return function(event) {
            var picFile = event.target;
            var div = document.getElementById("uploadboard");

            div.innerHTML = div.innerHTML + "<span class='preview'>
            <img id='"+ file.name +"' src='"+ picFile.result +" '/>
            <button class='remover'></button></span>";
            }
            )(f);
            }

            picReader.readAsDataURL(f);
            filesCollection[f.name] = f;
            }


            One more alternative is using the statement let




            The let statement declares a block scope local variable, optionally initializing it to a value.




            for (let i = 0; i < files.length; i++) {
            let file = files[i];
            let picReader = new FileReader();

            picReader.addEventListener("load", function (event) {
            let picFile = event.target;
            let div = document.getElementById("uploadboard");

            div.innerHTML = div.innerHTML + "<span class='preview'>
            <img id='"+ file.name +"' src='"+ picFile.result +" '/>
            <button class='remover'></button></span>";
            }

            picReader.readAsDataURL(file);
            filesCollection[f.name] = file;
            }





            share|improve this answer




























              0














              Because of the statement var




              The scope of a variable declared with var is its current execution context, which is either the enclosing function or, for variables declared outside any function, global.




              You can create a function using an approach with IIFE (Immediately Invoked Function Expression)



              for (var i = 0; i < files.length; i++) {
              var f = files[i];
              var picReader = new FileReader();

              picReader.addEventListener("load", (function (file) {
              return function(event) {
              var picFile = event.target;
              var div = document.getElementById("uploadboard");

              div.innerHTML = div.innerHTML + "<span class='preview'>
              <img id='"+ file.name +"' src='"+ picFile.result +" '/>
              <button class='remover'></button></span>";
              }
              )(f);
              }

              picReader.readAsDataURL(f);
              filesCollection[f.name] = f;
              }


              One more alternative is using the statement let




              The let statement declares a block scope local variable, optionally initializing it to a value.




              for (let i = 0; i < files.length; i++) {
              let file = files[i];
              let picReader = new FileReader();

              picReader.addEventListener("load", function (event) {
              let picFile = event.target;
              let div = document.getElementById("uploadboard");

              div.innerHTML = div.innerHTML + "<span class='preview'>
              <img id='"+ file.name +"' src='"+ picFile.result +" '/>
              <button class='remover'></button></span>";
              }

              picReader.readAsDataURL(file);
              filesCollection[f.name] = file;
              }





              share|improve this answer


























                0












                0








                0







                Because of the statement var




                The scope of a variable declared with var is its current execution context, which is either the enclosing function or, for variables declared outside any function, global.




                You can create a function using an approach with IIFE (Immediately Invoked Function Expression)



                for (var i = 0; i < files.length; i++) {
                var f = files[i];
                var picReader = new FileReader();

                picReader.addEventListener("load", (function (file) {
                return function(event) {
                var picFile = event.target;
                var div = document.getElementById("uploadboard");

                div.innerHTML = div.innerHTML + "<span class='preview'>
                <img id='"+ file.name +"' src='"+ picFile.result +" '/>
                <button class='remover'></button></span>";
                }
                )(f);
                }

                picReader.readAsDataURL(f);
                filesCollection[f.name] = f;
                }


                One more alternative is using the statement let




                The let statement declares a block scope local variable, optionally initializing it to a value.




                for (let i = 0; i < files.length; i++) {
                let file = files[i];
                let picReader = new FileReader();

                picReader.addEventListener("load", function (event) {
                let picFile = event.target;
                let div = document.getElementById("uploadboard");

                div.innerHTML = div.innerHTML + "<span class='preview'>
                <img id='"+ file.name +"' src='"+ picFile.result +" '/>
                <button class='remover'></button></span>";
                }

                picReader.readAsDataURL(file);
                filesCollection[f.name] = file;
                }





                share|improve this answer













                Because of the statement var




                The scope of a variable declared with var is its current execution context, which is either the enclosing function or, for variables declared outside any function, global.




                You can create a function using an approach with IIFE (Immediately Invoked Function Expression)



                for (var i = 0; i < files.length; i++) {
                var f = files[i];
                var picReader = new FileReader();

                picReader.addEventListener("load", (function (file) {
                return function(event) {
                var picFile = event.target;
                var div = document.getElementById("uploadboard");

                div.innerHTML = div.innerHTML + "<span class='preview'>
                <img id='"+ file.name +"' src='"+ picFile.result +" '/>
                <button class='remover'></button></span>";
                }
                )(f);
                }

                picReader.readAsDataURL(f);
                filesCollection[f.name] = f;
                }


                One more alternative is using the statement let




                The let statement declares a block scope local variable, optionally initializing it to a value.




                for (let i = 0; i < files.length; i++) {
                let file = files[i];
                let picReader = new FileReader();

                picReader.addEventListener("load", function (event) {
                let picFile = event.target;
                let div = document.getElementById("uploadboard");

                div.innerHTML = div.innerHTML + "<span class='preview'>
                <img id='"+ file.name +"' src='"+ picFile.result +" '/>
                <button class='remover'></button></span>";
                }

                picReader.readAsDataURL(file);
                filesCollection[f.name] = file;
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 2:20









                EleEle

                25.5k52251




                25.5k52251

















                    Popular posts from this blog

                    mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

                    How to change which sound is reproduced for terminal bell?

                    Can I use Tabulator js library in my java Spring + Thymeleaf project?