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

user2625363
  • 845
  • 2
  • 10
  • 15

4 Answers4

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

Community
  • 1
  • 1
ChaoticNadirs
  • 2,280
  • 2
  • 20
  • 27
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

bcarn
  • 133
  • 6
  • 1
    This `${'` should be `$('`. Missed quote, too: `file" />` – Al.G. Mar 26 '14 at 15:11
  • 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 Mar 26 '14 at 16:01
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>
alexdmejias
  • 1,399
  • 4
  • 19
  • 34
  • 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 Mar 26 '14 at 16:00
  • @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 – alexdmejias Mar 26 '14 at 16:53
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
BlackPearl
  • 2,532
  • 3
  • 33
  • 49