Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger file input dialog

How can I auto trigger file input? ie. in the link below I want to trigger upload button on load

DEMO

<form id="test_form">
    <input type="file" id="test">
    <div id="test1">
        <button>Upload</button>
    </div>
</form>
$("#test1").trigger('click');
$("#test").trigger('click');
like image 605
chicharito Avatar asked Nov 18 '15 10:11

chicharito


People also ask

How do you assign a value to a file in HTML?

The only way to set the value of a file input is by the user to select a file. This is done for security reasons. Otherwise you would be able to create a JavaScript that automatically uploads a specific file from the client's computer.


2 Answers

File input can't be automatically triggered in onload due to security purpose. It can't be fired without any user interaction. It is very disgusting when a page activates anything itself when the page loads.

By the way. You can use label instead of button like following:

<label for="test">Upload</label>
like image 180
Ibrahim Khan Avatar answered Sep 27 '22 19:09

Ibrahim Khan


$("document").ready(function() {
    setTimeout(function() {
        $("#test1").trigger('click');
    },10);

    $('#test1').click(function(){
        alert('hii');
    })
});

click event triggerd.

http://jsfiddle.net/j9oL4nyn/1/

like image 32
Dhara Avatar answered Sep 27 '22 19:09

Dhara