I have a file <input> field
and a <span>
decorates the input field:
<span class="span5 btn btn-primary btn-file" id="chose_files_btn" onclick="filechose_button.click()">chose files
<input id="filechose_button" type="file" name="fileData" size="1" style="display: none"/>
</span>
While the behavior of this is as I suppose in Chrome and Safari, FireFox opens two file input dialogs
on clicking the button(span)
.
Why could happen so?
I assume, that file input field is invisible and only access to it is through the span with a button behavior.
Update:
if I put the <input>
outside of <span>
it behaves normally.
<span class="span5 btn btn-primary btn-file" id="chose_files_btn" onclick="filechose_button.click()">chose files</span>
<input id="filechose_button" type="file" name="fileData" size="1" style="display: none"/>
but why on inside position
it does not?
I needed to use "unbind click" for my code work normally.
It is because of event propagation. When you click on the span, click event is raised and in the click handler you have called click on input type="file" so it is calling twice.
If you will try following code it would not raise propagated event.
For more information visit this link
You should play with this to get more understanding on event propagation.
It is because of some kind of event propagation mess
And
Demo: Fiddle
Seems like there could still be situations where the DOM bounces the event around, so the technique of hiding an input field and programming it to click is susceptible. I am now working on a modal dialog in an AngularJS app (designed to be used either on mobile with cordova or on a desktop browser) that needs to launch a file picker, where this phenomena happens and none of the above techniques helped.
When I place console logs on the bouncing event, it shows that the echo can arrive up to 1 second after the original click.
Following is a solution that overcomes it by creating a small stack of events, and eliminating duplicates that happen within 2 seconds.
Cheers, Z.