JavaScript javascript function on later created element javascript call function later,javascript run function later,javascript execute

I have the following html setup and cannot solve this issue.

When a button in the html document gets clicked, an element

<div id='wrapper'>
   <input id='fileUpload' name='fileUpload' type='file' onchange='uploadpreview(this.id)' multiple />
    <br />
    <div id='image-holder'></div>
   </div>

gets added using the function

function add(){
$j("<?php echo($divoutput)?>").insertBefore("#addbox");
}

(the php code has the html format in there).

However when this is done, the javascript function (Which is already in the html document and does not get added via a code) does not work:

$("#fileUpload").on('change', uploadpreview() {


     //Get count of selected files
  var countFiles = upload[0].files.length;

     var imgPath = upload[0].value;
     var extn = imgPath.substring(imgPath.lastIndexOf('.') + 1).toLowerCase();
     var image_holder = $("#image-holder");
     image_holder.empty();

     if (extn == "gif" || extn == "png" || extn == "jpg" || extn == "jpeg") {
         if (typeof (FileReader) != "undefined") {

             //loop for each file selected for uploaded.
             for (var i = 0; i < countFiles; i++) {

                 var reader = new FileReader();
                 reader.onload = function (e) {
                     $("<img />", {
                         "src": e.target.result,
                             "class": "thumb-image"
                     }).appendTo(image_holder);
                 }

                 image_holder.show();
                 reader.readAsDataURL(upload[0].files[i]);
             }

         } else {
             alert("This browser does not support FileReader.");
         }
     } else {
         alert("Pls select only images");
     }
 }

It throws an error that length cannot be read etc, even though a file has been uploaded.

What can be the trouble of this?

I have tried the code separately (without being added via a code) and everything works flawlessly.

Please help!

Answer:1

So I have a css grid layout with "boxes" inside, that I filter with a text input and javascript. How can I hide the filtered out elements so that they don't even take up space within the grid (so the ...

So I have a css grid layout with "boxes" inside, that I filter with a text input and javascript. How can I hide the filtered out elements so that they don't even take up space within the grid (so the ...

I want to generate HTML-code at the beginning of an existing element. This is the existing HTML-element: <div id="container"> <p>end of the element</p> </div> With my ...

I want to generate HTML-code at the beginning of an existing element. This is the existing HTML-element: <div id="container"> <p>end of the element</p> </div> With my ...

I need to discover the function 'foo' on class Something and it doesn't work. Is this because I am missing a subtlety of using class syntax or because of babel or something else? I would prefer to use ...

I need to discover the function 'foo' on class Something and it doesn't work. Is this because I am missing a subtlety of using class syntax or because of babel or something else? I would prefer to use ...

  1. javascript functions class
  2. private functions javascript class
  3. static functions javascript class
  4. functions inside javascript class
  5. anonymous functions javascript classes
  6. javascript functions first class objects
  7. javascript functions first class
  8. object function class javascript
  9. prototype function class javascript
  10. nested functions class javascript
  11. javascript functions as first class citizens
  12. arrow functions in javascript class
  13. import function into javascript class

This is incredibly annoying.. I am wondering why the heck my changes aren't reflected as I notice that my JavaScript file for my Web Worker always gets loaded from cache: I have disabled the Cache ...

This is incredibly annoying.. I am wondering why the heck my changes aren't reflected as I notice that my JavaScript file for my Web Worker always gets loaded from cache: I have disabled the Cache ...