I'm trying to trigger the click event of the <input type="file">
element from the button
.
<input id="upload"
type="file"
ng-file-select="onFileSelect($files)"
style="display: none;">
<button type="button"
ng-click="angular.element('#upload').trigger('click');">Upload</button>
It's common practice to hide the uglified beast known as <input type=file>
and trigger it's click event by some other means.
Simply have them in the same controller, and do something like this:
HTML:
JS:
This is, in my opinion, the best way to do it in angular. Using jQuery to find the element and trigger an event isn't the best practice.
One more directive
html
code
for jqLite just use triggerHandler with event name, To simulate a "click" try:
Here's the list of jQLite commands
best and simple way to use native java Script which is one liner code.
document.querySelector('#id').click();
Just add 'id' to your html element like
<button id="myId1" ng-click="someFunction()"></button>
check condition in javascript code
if(condition) { document.querySelector('#myId1').click(); }
I had this same issue and this fiddle is the shizzle :) It uses a directive to properly style the file field and you can even make it an image or whatever.
http://jsfiddle.net/stereosteve/v5Rdc/7/
The solution, as pointed out by other answers, is to use
Here's an example of how I randomly select multiple
select
elements: