title: javascript function on later created element
body: <p>I have the following html setup and cannot solve this issue. </p>
  
  <p>When a button in the html document gets clicked, an element </p>
  
  <pre><code>&lt;div id='wrapper'&gt;
     &lt;input id='fileUpload' name='fileUpload' type='file' onchange='uploadpreview(this.id)' multiple /&gt;
      &lt;br /&gt;
      &lt;div id='image-holder'&gt;&lt;/div&gt;
     &lt;/div&gt;
  </code></pre>
  
  <p>gets added using the function</p>
  
  <pre><code>function add(){
  $j("&lt;?php echo($divoutput)?&gt;").insertBefore("#addbox");
  }
  </code></pre>
  
  <p>(the php code has the html format in there).</p>
  
  <p>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:</p>
  
  <pre><code>$("#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 &lt; countFiles; i++) {
  
                   var reader = new FileReader();
                   reader.onload = function (e) {
                       $("&lt;img /&gt;", {
                           "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");
       }
   }
  </code></pre>
  
  <p>It throws an error that length cannot be read etc, even though a file has been uploaded. </p>
  
  <p>What can be the trouble of this?</p>
  
  <p>I have tried the code separately (without being added via a code) and everything works flawlessly. </p>
  
  <p>Please help!</p>