0

Onclick a button I get a file input box as array.

<input type='button' class='tiny button radius' value='+' id='addButton' data-id='".$v->quid."' data-role='image'>




 $("#addButton").click(function () {

        var newTextBoxDiv = $(document.createElement('div'));
        newTextBoxDiv.after().html('<div class="row">
<div class="large-4 columns left button-mar-top-btm">
<label class="tiny button radius multipleimg">
<input type="file" name="ques_'+quid+'[]' + '" id="ques_'+quid+'[]' + '">Upload Photo</label> </div>
<div class="large-6 columns button-mar-top-btm">
<input class="file-upload-input" type="text"></div>
<div class="large-2 left columns button-mar-top-btm">
<a href="#" class="removeField" >Remove</a></div></div>');
        });

so the above code will generate the input box with name 'ques_32[]' multiple times.

What I am trying to do is to put the chosen file value in a textbox on each click.

1

2 Answers 2

2

It's actually as easy as just binding the change event on the file input, get the value and set it on a text input.

$('#ques_32\\[\\]').on('change', function() {
    $('#filename').val(this.value)
});

You won't get the real path for security reasons, so you'll end up with something like /fakepath/filename.png etc.

For multiple files and to just get the filenames, as you can't get the path anyway, you can do something like

$('#ques_32\\[\\]').on('change', function(e) {
    var filenames = [].slice.call(e.target.files).map(function(f) {
        return f.name;
    }).join(', ')

    $('#filename').val(filenames);
});

FIDDLE

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

7 Comments

Thanks a lot for showing the path. It worked great.Only one problem is I have the input field as array now it is not detecting which file is clicked.
You'll have to explain that better? In an array how exactly, and detect each file how ?
Now it's changed.Basically the file input can be more than one so how it can detect which input has been clicked.
You'll have to target each one, or all of them with a class etc ?
That's what I am thinking for last 4 hrs....there will be a common class and onclick that class it will take the id of that particular file and then get the filename and put it in another textbox with same id.
|
0

It is possible using HTML5 (i.e, not supported on older browsers): See http://blog.teamtreehouse.com/reading-files-using-the-html5-filereader-api

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.