0

I have an input element (Dropzone) that's hidden, and is written in the original html's body (was not appended).

<div style="display: none;">
    <form action="/uploadProfile" method="post" class="dropzone" id="uploadProfileDropzone"></form>
</div>

Now inside my .js script, I'm trying:

 $(function() {
      $('#uploadProfileDropzone').click()
 })

And nothing happens. However, if I'm calling $('#uploadProfileDropzone').click() inside Chrome's console, it works.

What could possibly be the problem?

EDIT:

Problem might be that I'm trying to call the function before my Dropzone has initialized. Is there a way to know when this happens?

However, even when trying:

$(function() {
    setTimeout(function() {
         $('#uploadProfileDropzone').click() 
    }, 5000)
})

Which is a lot after the page fully loads, still nothing happens

SOLUTION:

It turns out that some (or even most of the) browsers block such activity. It's pretty obvious why, looking back at it now. My intention was to navigate to a page that once ready, opens a file dialog for a profile upload action. It seems logically correct to block such action from a user experience point of view, as it can lead to spam and undesired activity from websites. I solved it by displaying a simple popup box on load, that forces the user to press a button, that in turn calls $('#uploadProfileDropzone').click() and it worked.

UFC Insider
  • 838
  • 1
  • 7
  • 19

3 Answers3

2

You possibly have two options:

1) Programatically create your dropzone

Dropzone.autoDiscover = false;
let csvDropzone = new Dropzone("#uploadProfileDropzone", {
  paramName: "file",
  init: function() {
      $('#uploadProfileDropzone').click();
  }
});
<div style="display: none;">
    <form action="/uploadProfile" method="post" class="dropzone" id="uploadProfileDropzone">
    </form>
</div>

2) You can try using init configuration method directly without initialising the dropzone in your JS code:

// Taken from the dropzone config page
Dropzone.options.uploadProfileDropzone = {
  init: function() {
      $('#uploadProfileDropzone').click();
  }
};
fixatd
  • 1,394
  • 1
  • 11
  • 19
  • tried both options, didn't work i'm starting to think maybe the browser is blocking such action? navigativing to a url that automatically opens a file dialog – UFC Insider Nov 27 '18 at 17:10
  • might be the case, have you tried in a different browser? – fixatd Nov 27 '18 at 19:35
  • 1
    yep, that was the case. i solved it by letting the user click a button before opening the file dialog – UFC Insider Nov 27 '18 at 21:10
0

It turns out that some (or even most of the) browsers block such activity. It's pretty obvious why, looking back at it now. My intention was to navigate to a page that once ready, opens a file dialog for a profile upload action. It seems logically correct to block such action from a user experience point of view, as it can lead to spam and undesired activity from websites. I solved it by displaying a simple popup box on load, that forces the user to press a button, that in turn calls $('#uploadProfileDropzone').click() and it worked.

UFC Insider
  • 838
  • 1
  • 7
  • 19
-3

you should use document ready event (code works once elements are initialized)

$(document).ready(function(){
     $('#uploadProfileDropzone').click(function(){
        // Do something here
    });
});

OR

$('#uploadProfileDropzone').on('click',function(){ write your code here });
Shahnewaz
  • 360
  • 4
  • 12
Chandrakant
  • 1,971
  • 1
  • 14
  • 33