2

How I can call "click" event on input type="file" by calling "context" event on other element?

I am trying this code:

HTML Markup:

<html>
    <head>
        <title>Title</title>
        <script type="text/javascript" src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
        <script type="text/javascript" src="js.js"></script>
</head>
<body>
    <input type="file" id="file"/>
    <button id="trigger">Click</button>
</body>

JavaScript file:

window.onload = function() {
window.oncontextmenu = function(){
    return false;
};
$("#trigger").on("contextmenu", function(){
    $("#file").trigger("click");
});

}

But I haven't got window to choose file, when I click the right mouse button on button with id="trigger".

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
hcxbvifwmrss
  • 31
  • 1
  • 5
  • Have a look at [this](http://stackoverflow.com/questions/706655/bind-event-to-right-mouse-click) thread – empiric Nov 13 '14 at 12:54
  • The event handler is triggered, but the file input won't open, changing the event from `contextmenu` to `click` makes it work, so this is clearly a security "feature" built into the browser, the same way a file input that is hidden can't be triggered for security reasons. – adeneo Nov 13 '14 at 12:54
  • 1
    http://jsfiddle.net/DSARd/1292/ – adeneo Nov 13 '14 at 12:57
  • The main idea is to use oncontext event. – hcxbvifwmrss Nov 13 '14 at 14:36

2 Answers2

1

Resolved!

  window.onload = function() {
        $("#trigger").mousedown(function(e){
            if(e.button == 2){
                $("#file").trigger("click");
            }
        });
    }
hcxbvifwmrss
  • 31
  • 1
  • 5
0

try like this:

Script:

$('#trigger').click(function() {
    $('#file').trigger('click');
});