jquery - TypeError: $(...).get(...) is undefined when trying to open a .txt file with input type text -


i'm trying read .txt file, split in lines , console.log it. far i'm getting error when try read file. please tell me i'm doing wrong.

$("#draftloginput").change(function () {        //gets first file draftlog      var filenumber = $('#draftlog').get(0).files.length;      filenumber--;      logfile = $('#draftlog').get(0).files[filenumber];        //reads file draftlog text      var reader = new filereader();      reader.readastext(logfile);      reader.onload = function (e) {          var rawlog = reader.result;            //splits text array of strings every new line          var re = /\r\n|\n\r|\n|\r/g;          var splitlog = rawlog.replace(re, "\n").split("\n");            console.log(splitlog);      };  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <input type="file" id="draftloginput">

instead of using

   var filenumber = $('#draftlog').get(0).files.length;     filenumber--;     logfile = $('#draftlog').get(0).files[filenumber]; 

use:

   var filenumber = $('#draftloginput')[0].files.length;     filenumber--;     logfile = $('#draftloginput')[0].files[filenumber]; 

you have no element id dreaftlog. selector getting empty array.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -