0

i have an input array to select multiple files.

<input type="file" name="files[]" multiple='multiple'/>
<div id='my_div'>Filenames</div>

How do i get the filenames using jquery? For text types, the map function works

var values=$("input[name='text_string[]']").map(function(){return $(this).val();}).get();

Is there any way to get filenames on clicking div?

3
  • Possible duplicate of Get all values of multiple file select with jQuery Commented Jun 13, 2019 at 10:43
  • just change your selector to $("input[type='file']") Commented Jun 13, 2019 at 10:46
  • it gives only the first fiename Commented Jun 13, 2019 at 10:50

1 Answer 1

1

You can access the files array of the control and then map() to create an array of the individual file names, something like this:

$('input').on('change', function() {
  var filenames = Array.from(this.files).map(function(f) {
    return f.name;
  });
  console.log(filenames);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="file" name="files[]" multiple='multiple' />

Sign up to request clarification or add additional context in comments.

3 Comments

Instead of input change, can i get the value on a div or button click?
Absolutely, I just used the change handler as an example. You can reference the element in any way you like
ok. it works. but it wont help me. because... first i select 2 images and on second time i select another 2 images first two gets cleared. so i used to append input type file with different id each time. so i want to fetch all image names from input type files from the name files[]. so finally i will get 4 images.

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.