<label for="input">Label</label><input type="file" id="input"/>
In Firefox 7 it is not possible to trigger the open file dialog by clicking on the label.
This SO question is very similar but that's green checked with it's a bug in FF. I'm looking for a workaround.
Any ideas?
thank you for this q&a... helped me out.
my variation of @marten-wikstrom's solution:
notes
$(function() {...});
) is unnecessary, in either solution.jQuery.fn.live
takes care of that in @marten-wikstrom's case; explicitly binding todocument
does in my example.jQuery.fn.on
... current recommended binding technique.added the
!== 'INPUT'
check to ensure execution does not get caught in a loop here:(since the file field click will bubble back up to the label)
change
event.target
check toevent.currentTarget
, allowing for initial click on the<em>
in:control
attribute for cleaner, simpler, spec-base form field association.I came up with a feasible workaround:
Quite strange that FF allows you to simulate a click on a file input. I thought that was considered a security risk...
UPDATE: This is a generic workaround:
It seems to be fixed in FF 23, so browser detection becomes hazardous and leads to double system dialogs ;(
You can add another test to restrict the fix to FF version prior to version 23:
It's quite ugly, but this fix will be removed as soon as old the version of FF will have disappeared.
A couple problems arise when using the jQuery browser detection, most notably the anti-pattern of using browser detection rather than feature detection, in addition to the fact that 1.9+ doesn't provide that functionality.
Perhaps, then, the solution I arrived at is a bit hypocritical, but it worked well and seems to adhere to most best practices today.
First, ensure you're using Paul Irish's conditional classes. Then, use something like:
Otherwise, I found the event would be double-fired in browsers such as Chrome. This solution seemed elegant enough.
Try this code
The file-selection dialog can be triggered in all browsers by the
click()
event. An unobtrusive solution to this problem could look like that:Removing the
for
attribute is important since other browsers (e.g. Chrome, IE) will still ratify it and show the dialog twice.I tested it in Chrome 25, Firefox 19 and IE 9 and works like a charm.