Maximum call [...] when trying to open file dialog

1

HTML

<div class="send">click<input name="files[]" class="file" type="file"></div>

Javascript:

$('body').on('click', '.send', function(e) {
     $(document).find('.file').click();
});

CSS:

 input {
   opacity: 0;
 }

Clicking on "click" is returned error, as it detects click on the parent div and daughter div causing a loop, how to solve? I just want to open the input file.

ex: link

    
asked by anonymous 12.03.2016 / 21:20

1 answer

2

The problem is that when you do .find('.file').click(); it will trigger an event in the parent element too, by propagation in the DOM.

You have to join another event handset that stops the event propagation to the parent element, just in the input.

$('body').on('click', '.send_file', function(e) {
    e.stopPropagation();
});

jsFiddle: link

But the way it seems best to do this is without JavaScript. Using only label like this:

<label class="send normal_send">click
    <input name="files[]" class="send_file" type="file">
</label>

jsFiddle: link

    
13.03.2016 / 00:59