0

I want to get the selected files names from the input element. here is the html element:

 <input id="camera-open" type="file" multiple>

and here is jquery i am trying to do:

    $('#camera-open').change(function(){        
       var names = [];
       $('input[type=file]').each(function(){
       names = $('input[type=file]').val().split('/').pop().split('\\').pop();    
});

But it only gives me one file name! Any idea?

1
  • declare var names[] initially, every time during change it get's initialize to null Commented Apr 9, 2017 at 13:45

2 Answers 2

1

try with

//step by step
$( '#camera-open' ).change( function( event ){        
       var fileList = Array.prototype.slice.call( event.target.files );       
       var filePaths = fileList.map( function(file){ return file.name; });
       var onlyNames = filePaths.map( function(file){ return file.split('/').pop().split('\\').pop(); });
       console.log( onlyNames );
});
Sign up to request clarification or add additional context in comments.

Comments

0

Try initialising the variables outside the loop,

var names = [],
    fileName;
$('#camera-open').change(function(){        
  $('input[type=file]').each(function(){
    fileName = $(this).val().split('/').pop().split('\\').pop();
    names.push(fileName);
  });
});
console.log(names);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.