Javascript getElementById but keep old value










-1















I have a file_field_tag inside a rails form with a select file field:



<%= file_field_tag "attachments[media_files]", multiple: true, id: "files" %>


And I have an area to preview the images/videos and remove if need be:



<span id="result"></span>


Everything is working correct but there is only one glitch.... If the images/videos are in separate folders, I have to add the files from one folder first and then from the other folder. The files show at the preview, but after this process only the second batch of files gets saved when I submit the form.
Here is the javascript for all of the above:



window.onload = function()
if(window.File && window.FileList && window.FileReader)


var filesInput = document.getElementById("files");


filesInput.addEventListener("change", function(event)


var files = event.target.files;
var output = document.getElementById("result");

for(var i = 0; i< files.length; i++)
exif)$/i))
continue;

var picReader = new FileReader();

picReader.addEventListener("load",function(event)

var picFile = event.target;

var span = document.createElement("span");

span.innerHTML = ['<img class="thumb" src="', picFile.result, '" title="', picFile.name, '"/><span class="remove_img_preview"></span>'].join('');



output.insertBefore(span,null);
span.children[1].addEventListener("click", function(event)
span.parentNode.removeChild(span);
);

);

picReader.readAsDataURL(file);


);

else

console.log("Your browser does not support File API");











share|improve this question
























  • Is it input type ='file'?

    – brk
    Nov 14 '18 at 14:29











  • Can you provide more of a code example? You could use localStorage, or indexDB to store each value.

    – Harry Chilinguerian
    Nov 14 '18 at 14:30











  • Thanks for the reply @brk , yes that is correct the input is input type ='file'

    – Theopap
    Nov 14 '18 at 14:31






  • 1





    @Theopap tru by this <input type="file" name="img" multiple> you can upload multiple file at once

    – brk
    Nov 14 '18 at 14:33











  • @brk I do have the input set as multiple: true, it's a rails input field.. btw I uploaded the full code.

    – Theopap
    Nov 14 '18 at 14:35















-1















I have a file_field_tag inside a rails form with a select file field:



<%= file_field_tag "attachments[media_files]", multiple: true, id: "files" %>


And I have an area to preview the images/videos and remove if need be:



<span id="result"></span>


Everything is working correct but there is only one glitch.... If the images/videos are in separate folders, I have to add the files from one folder first and then from the other folder. The files show at the preview, but after this process only the second batch of files gets saved when I submit the form.
Here is the javascript for all of the above:



window.onload = function()
if(window.File && window.FileList && window.FileReader)


var filesInput = document.getElementById("files");


filesInput.addEventListener("change", function(event)


var files = event.target.files;
var output = document.getElementById("result");

for(var i = 0; i< files.length; i++)
exif)$/i))
continue;

var picReader = new FileReader();

picReader.addEventListener("load",function(event)

var picFile = event.target;

var span = document.createElement("span");

span.innerHTML = ['<img class="thumb" src="', picFile.result, '" title="', picFile.name, '"/><span class="remove_img_preview"></span>'].join('');



output.insertBefore(span,null);
span.children[1].addEventListener("click", function(event)
span.parentNode.removeChild(span);
);

);

picReader.readAsDataURL(file);


);

else

console.log("Your browser does not support File API");











share|improve this question
























  • Is it input type ='file'?

    – brk
    Nov 14 '18 at 14:29











  • Can you provide more of a code example? You could use localStorage, or indexDB to store each value.

    – Harry Chilinguerian
    Nov 14 '18 at 14:30











  • Thanks for the reply @brk , yes that is correct the input is input type ='file'

    – Theopap
    Nov 14 '18 at 14:31






  • 1





    @Theopap tru by this <input type="file" name="img" multiple> you can upload multiple file at once

    – brk
    Nov 14 '18 at 14:33











  • @brk I do have the input set as multiple: true, it's a rails input field.. btw I uploaded the full code.

    – Theopap
    Nov 14 '18 at 14:35













-1












-1








-1








I have a file_field_tag inside a rails form with a select file field:



<%= file_field_tag "attachments[media_files]", multiple: true, id: "files" %>


And I have an area to preview the images/videos and remove if need be:



<span id="result"></span>


Everything is working correct but there is only one glitch.... If the images/videos are in separate folders, I have to add the files from one folder first and then from the other folder. The files show at the preview, but after this process only the second batch of files gets saved when I submit the form.
Here is the javascript for all of the above:



window.onload = function()
if(window.File && window.FileList && window.FileReader)


var filesInput = document.getElementById("files");


filesInput.addEventListener("change", function(event)


var files = event.target.files;
var output = document.getElementById("result");

for(var i = 0; i< files.length; i++)
exif)$/i))
continue;

var picReader = new FileReader();

picReader.addEventListener("load",function(event)

var picFile = event.target;

var span = document.createElement("span");

span.innerHTML = ['<img class="thumb" src="', picFile.result, '" title="', picFile.name, '"/><span class="remove_img_preview"></span>'].join('');



output.insertBefore(span,null);
span.children[1].addEventListener("click", function(event)
span.parentNode.removeChild(span);
);

);

picReader.readAsDataURL(file);


);

else

console.log("Your browser does not support File API");











share|improve this question
















I have a file_field_tag inside a rails form with a select file field:



<%= file_field_tag "attachments[media_files]", multiple: true, id: "files" %>


And I have an area to preview the images/videos and remove if need be:



<span id="result"></span>


Everything is working correct but there is only one glitch.... If the images/videos are in separate folders, I have to add the files from one folder first and then from the other folder. The files show at the preview, but after this process only the second batch of files gets saved when I submit the form.
Here is the javascript for all of the above:



window.onload = function()
if(window.File && window.FileList && window.FileReader)


var filesInput = document.getElementById("files");


filesInput.addEventListener("change", function(event)


var files = event.target.files;
var output = document.getElementById("result");

for(var i = 0; i< files.length; i++)
exif)$/i))
continue;

var picReader = new FileReader();

picReader.addEventListener("load",function(event)

var picFile = event.target;

var span = document.createElement("span");

span.innerHTML = ['<img class="thumb" src="', picFile.result, '" title="', picFile.name, '"/><span class="remove_img_preview"></span>'].join('');



output.insertBefore(span,null);
span.children[1].addEventListener("click", function(event)
span.parentNode.removeChild(span);
);

);

picReader.readAsDataURL(file);


);

else

console.log("Your browser does not support File API");








javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 15:25







Theopap

















asked Nov 14 '18 at 14:27









TheopapTheopap

258113




258113












  • Is it input type ='file'?

    – brk
    Nov 14 '18 at 14:29











  • Can you provide more of a code example? You could use localStorage, or indexDB to store each value.

    – Harry Chilinguerian
    Nov 14 '18 at 14:30











  • Thanks for the reply @brk , yes that is correct the input is input type ='file'

    – Theopap
    Nov 14 '18 at 14:31






  • 1





    @Theopap tru by this <input type="file" name="img" multiple> you can upload multiple file at once

    – brk
    Nov 14 '18 at 14:33











  • @brk I do have the input set as multiple: true, it's a rails input field.. btw I uploaded the full code.

    – Theopap
    Nov 14 '18 at 14:35

















  • Is it input type ='file'?

    – brk
    Nov 14 '18 at 14:29











  • Can you provide more of a code example? You could use localStorage, or indexDB to store each value.

    – Harry Chilinguerian
    Nov 14 '18 at 14:30











  • Thanks for the reply @brk , yes that is correct the input is input type ='file'

    – Theopap
    Nov 14 '18 at 14:31






  • 1





    @Theopap tru by this <input type="file" name="img" multiple> you can upload multiple file at once

    – brk
    Nov 14 '18 at 14:33











  • @brk I do have the input set as multiple: true, it's a rails input field.. btw I uploaded the full code.

    – Theopap
    Nov 14 '18 at 14:35
















Is it input type ='file'?

– brk
Nov 14 '18 at 14:29





Is it input type ='file'?

– brk
Nov 14 '18 at 14:29













Can you provide more of a code example? You could use localStorage, or indexDB to store each value.

– Harry Chilinguerian
Nov 14 '18 at 14:30





Can you provide more of a code example? You could use localStorage, or indexDB to store each value.

– Harry Chilinguerian
Nov 14 '18 at 14:30













Thanks for the reply @brk , yes that is correct the input is input type ='file'

– Theopap
Nov 14 '18 at 14:31





Thanks for the reply @brk , yes that is correct the input is input type ='file'

– Theopap
Nov 14 '18 at 14:31




1




1





@Theopap tru by this <input type="file" name="img" multiple> you can upload multiple file at once

– brk
Nov 14 '18 at 14:33





@Theopap tru by this <input type="file" name="img" multiple> you can upload multiple file at once

– brk
Nov 14 '18 at 14:33













@brk I do have the input set as multiple: true, it's a rails input field.. btw I uploaded the full code.

– Theopap
Nov 14 '18 at 14:35





@brk I do have the input set as multiple: true, it's a rails input field.. btw I uploaded the full code.

– Theopap
Nov 14 '18 at 14:35












1 Answer
1






active

oldest

votes


















1














Depending on what you're trying to achieve you could just save the files into an array variable:






////////////////////////////////
// Create an array for the files somewhere
var fileCache = ;
////////////////////////////////

window.onload = function()
if(window.File && window.FileList && window.FileReader)


var filesInput = document.getElementById("files");


filesInput.addEventListener("change", function(event)


var files = event.target.files;
var output = document.getElementById("result");

for(var i = 0; i< files.length; i++)
avi
);

else

console.log("Your browser does not support File API");


<input type="file" id="files" />





Generally, I would avoid using a random global variable, but without knowing exactly what you want to do with the list of files this is the most generic answer.






share|improve this answer























  • Thanks fro the reply @Ricardo Reyna, I've updated my question to exactly what i want. Unfortunately with the answer you provided it has the same behavior as before

    – Theopap
    Nov 14 '18 at 15:25











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302507%2fjavascript-getelementbyid-but-keep-old-value%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Depending on what you're trying to achieve you could just save the files into an array variable:






////////////////////////////////
// Create an array for the files somewhere
var fileCache = ;
////////////////////////////////

window.onload = function()
if(window.File && window.FileList && window.FileReader)


var filesInput = document.getElementById("files");


filesInput.addEventListener("change", function(event)


var files = event.target.files;
var output = document.getElementById("result");

for(var i = 0; i< files.length; i++)
avi
);

else

console.log("Your browser does not support File API");


<input type="file" id="files" />





Generally, I would avoid using a random global variable, but without knowing exactly what you want to do with the list of files this is the most generic answer.






share|improve this answer























  • Thanks fro the reply @Ricardo Reyna, I've updated my question to exactly what i want. Unfortunately with the answer you provided it has the same behavior as before

    – Theopap
    Nov 14 '18 at 15:25
















1














Depending on what you're trying to achieve you could just save the files into an array variable:






////////////////////////////////
// Create an array for the files somewhere
var fileCache = ;
////////////////////////////////

window.onload = function()
if(window.File && window.FileList && window.FileReader)


var filesInput = document.getElementById("files");


filesInput.addEventListener("change", function(event)


var files = event.target.files;
var output = document.getElementById("result");

for(var i = 0; i< files.length; i++)
avi
);

else

console.log("Your browser does not support File API");


<input type="file" id="files" />





Generally, I would avoid using a random global variable, but without knowing exactly what you want to do with the list of files this is the most generic answer.






share|improve this answer























  • Thanks fro the reply @Ricardo Reyna, I've updated my question to exactly what i want. Unfortunately with the answer you provided it has the same behavior as before

    – Theopap
    Nov 14 '18 at 15:25














1












1








1







Depending on what you're trying to achieve you could just save the files into an array variable:






////////////////////////////////
// Create an array for the files somewhere
var fileCache = ;
////////////////////////////////

window.onload = function()
if(window.File && window.FileList && window.FileReader)


var filesInput = document.getElementById("files");


filesInput.addEventListener("change", function(event)


var files = event.target.files;
var output = document.getElementById("result");

for(var i = 0; i< files.length; i++)
avi
);

else

console.log("Your browser does not support File API");


<input type="file" id="files" />





Generally, I would avoid using a random global variable, but without knowing exactly what you want to do with the list of files this is the most generic answer.






share|improve this answer













Depending on what you're trying to achieve you could just save the files into an array variable:






////////////////////////////////
// Create an array for the files somewhere
var fileCache = ;
////////////////////////////////

window.onload = function()
if(window.File && window.FileList && window.FileReader)


var filesInput = document.getElementById("files");


filesInput.addEventListener("change", function(event)


var files = event.target.files;
var output = document.getElementById("result");

for(var i = 0; i< files.length; i++)
avi
);

else

console.log("Your browser does not support File API");


<input type="file" id="files" />





Generally, I would avoid using a random global variable, but without knowing exactly what you want to do with the list of files this is the most generic answer.






////////////////////////////////
// Create an array for the files somewhere
var fileCache = ;
////////////////////////////////

window.onload = function()
if(window.File && window.FileList && window.FileReader)


var filesInput = document.getElementById("files");


filesInput.addEventListener("change", function(event)


var files = event.target.files;
var output = document.getElementById("result");

for(var i = 0; i< files.length; i++)
avi
);

else

console.log("Your browser does not support File API");


<input type="file" id="files" />





////////////////////////////////
// Create an array for the files somewhere
var fileCache = ;
////////////////////////////////

window.onload = function()
if(window.File && window.FileList && window.FileReader)


var filesInput = document.getElementById("files");


filesInput.addEventListener("change", function(event)


var files = event.target.files;
var output = document.getElementById("result");

for(var i = 0; i< files.length; i++)
avi
);

else

console.log("Your browser does not support File API");


<input type="file" id="files" />






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 15:15









Ricardo ReynaRicardo Reyna

524614




524614












  • Thanks fro the reply @Ricardo Reyna, I've updated my question to exactly what i want. Unfortunately with the answer you provided it has the same behavior as before

    – Theopap
    Nov 14 '18 at 15:25


















  • Thanks fro the reply @Ricardo Reyna, I've updated my question to exactly what i want. Unfortunately with the answer you provided it has the same behavior as before

    – Theopap
    Nov 14 '18 at 15:25

















Thanks fro the reply @Ricardo Reyna, I've updated my question to exactly what i want. Unfortunately with the answer you provided it has the same behavior as before

– Theopap
Nov 14 '18 at 15:25






Thanks fro the reply @Ricardo Reyna, I've updated my question to exactly what i want. Unfortunately with the answer you provided it has the same behavior as before

– Theopap
Nov 14 '18 at 15:25




















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302507%2fjavascript-getelementbyid-but-keep-old-value%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto