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".

4
  • Have a look at this thread Commented Nov 13, 2014 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. Commented Nov 13, 2014 at 12:54
  • 1
    jsfiddle.net/DSARd/1292 Commented Nov 13, 2014 at 12:57
  • The main idea is to use oncontext event. Commented Nov 13, 2014 at 14:36

2 Answers 2

1

Resolved!

  window.onload = function() {
        $("#trigger").mousedown(function(e){
            if(e.button == 2){
                $("#file").trigger("click");
            }
        });
    }
Sign up to request clarification or add additional context in comments.

Comments

0

try like this:

Script:

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

1 Comment

The main idea is to use oncontext event on button with id="trigger".

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.