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;
}
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;
}
javascript jquery ajax
marked as duplicate by charlietfl
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.
add a comment |
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;
}
javascript jquery ajax
marked as duplicate by charlietfl
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.
add a comment |
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;
}
javascript jquery ajax
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
javascript jquery ajax
asked Nov 23 '18 at 2:09
Hugo Di Mark DhersHugo Di Mark Dhers
66
66
marked as duplicate by charlietfl
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
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.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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;
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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;
}
add a comment |
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;
}
add a comment |
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;
}
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;
}
answered Nov 23 '18 at 2:20
EleEle
25.5k52251
25.5k52251
add a comment |
add a comment |