2

I am trying to trigger input tag to select a file by a button click though jquery. code example like the following

<input type="file" id="input_file"/>

<button onclick="select_file()"/>

function select_file() {
${'input_file').select("http://www.someimage.com/image.gif");
}

After hours of research, I still have not got the solution how I could make this happen.

Appreciate for any suggestion or solution

Thanks you

3
  • 3
    the file input type is used to select file from local file system... Commented Mar 26, 2014 at 14:58
  • Your selector is wrong..you have a curly brace where you need a parenthesis Commented Mar 26, 2014 at 14:58
  • @ArunPJohny is correct. Are you trying to upload a file from the user's computer or are you having the user select an image available online (and then want to upload that image)? Commented Mar 28, 2014 at 15:12

4 Answers 4

1

I was going to suggest:

$("#input-file").trigger("click");

but then I remembered there are security restrictions which prevent triggering and setting the values of file input dialogs.

There are some ways around it though, take a look at this question: https://stackoverflow.com/a/3030174/992435

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

Comments

0

You have typos in your codes... correct as follows;

<input type="file" id="input_file"/>

<button onclick="select_file()"/>

function select_file() {

   $('#input_file').val("http://www.someimage.com/image.gif");

}
  1. Use Val in place of Select
  2. You missed a # sign in the function
  3. You missed a quote in the file input tag
  4. You used a curly brace instead of a bracket

Comments

0

There were a couple of things wrong with your snippet. You were not using script tags, you had forgotten the '#' before ID selectors and you had a curly bracket after the dollar sign.

<input type="file" id="input_file/>

<button onclick="select_file()"/>
<script>    
function select_file() {
    $('#input_file').select("http://www.someimage.com/image.gif");
}
</script>

Although the way you are approaching your problem technically works, I would never recommend it since it can lead to unmaintainable code. A better approach would be something like:

<input type="file" id="input_file/>

<button class="changeInputButton" />
<script>    
$('.changeInputButton').on('click', function () {
  $('#input_file').val("http://www.someimage.com/image.gif");
});
</script>

2 Comments

Uncaught InvalidStateError: Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programatically set to the empty string. ???
@user2625363 well I'm not sure what you are trying to accomplish. The warning you are getting is telling you that you can't select a random file in the system file of the user programmatically as it would be a huge security concern
0

You should use

<input type="hidden" id="input_file"/>
<button onclick="select_file()"/>

function select_file() {
 $('input_file').val("http://www.someimage.com/image.gif");
}

The input type of file is used mainly for local file system files.

EDIT: Sorry about the typos

2 Comments

This ${' should be $('. Missed quote, too: file" />
Uncaught InvalidStateError: Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programatically set to the empty string. ?

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.