1

I am trying to get the filename of the uploaded file into a textbox via jquery. It should be easy enough if there is single file upload button.

But I have file input as an array and beside each file upload button there is a textbox.

I have created a fiddle which will demonstrate more clearly. The problem is I can't get the value/name of the selected file on change function.

$('.imgupload').on('change', function (e) {
    var filenames = [].slice.call(e.target.files).map(function (f) {
        alert(f.name);
        return f.name;
    });
    $('#filename').val(filenames);
});

JSFiddle -- http://jsfiddle.net/squidraj/Ldcp7hoc/4/

0

1 Answer 1

3

You are close, but there is no input with id="filename", you do have filename1, filename2 and data-id variables. So just grab the data-id and give the corresponding input (first, second, etc.) the value equal to your var filename which you were getting correctly.

$('#ques_32\\[\\]').on('change', function (e) {
    var filename = [].slice.call(e.target.files).map(function (f) {
        return f.name;
    });
    $('#filename' + $(this).attr("data-id")).val(filename);
});

Updated Fiddle

By the way you had a typo in your fiddle, wrote dataid instead of data-id

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

1 Comment

Hey Dan...that's awesome. Brilliant!! Thanks a lot for saving my rest of the night :p

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.