Skip to content

Commit 8fdecc9

Browse files
author
Alex Kudlick
committed
Allow display of labels instead of input tags
Since most browsers don't allow web developers to apply styles to file inputs, it's common practice to show a styled label and position the file input off the screen. This commit allows FileReader to work with such a setup. e.g. if we had: ```html <label style="background-color: red; font-color: blue;"> Click Me! <input type="file" style="position: absolute; left: -99999px" /> </label> ``` the user would see and interact with our styled 'Click Me' text, instead of the default file input.
1 parent 4cfe5b9 commit 8fdecc9

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

jquery.FileReader.js

+21-13
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,36 @@
5050
var main = function(el, options) {
5151
return el.each(function(i, input) {
5252
input = $(input);
53-
var id = input.attr('id');
54-
if (!id) {
55-
id = 'flashFileInput' + inputsCount;
56-
input.attr('id', id);
53+
var inputId = input.attr('id'),
54+
label,
55+
clickableElement;
56+
57+
if (!inputId) {
58+
inputId = 'flashFileInput' + inputsCount;
5759
inputsCount++;
5860
}
61+
label = input.parent('label');
62+
clickableElement = label.length > 0 ? label : input;
63+
clickableElement.attr('id', clickableElement.attr('id') || inputId);
64+
65+
5966
options.multiple = !!(options.multiple === null ? input.attr('multiple') : options.multiple);
6067
options.accept = options.accept === null ? input.attr('accept') : options.accept;
6168

62-
FileAPIProxy.inputs[id] = input;
63-
FileAPIProxy.swfObject.add(id, options.multiple, options.accept, options.label, options.extensions);
69+
FileAPIProxy.inputs[inputId] = input;
70+
FileAPIProxy.swfObject.add(clickableElement.attr('id'), options.multiple, options.accept, options.label, options.extensions);
6471

65-
input.css('z-index', 0)
72+
73+
clickableElement.css('z-index', 0)
6674
.mouseover(function (e) {
67-
if (id !== currentTarget) {
75+
if (inputId !== currentTarget) {
6876
e = e || window.event;
69-
currentTarget = id;
70-
FileAPIProxy.swfObject.mouseover(id);
77+
currentTarget = inputId;
78+
FileAPIProxy.swfObject.mouseover(clickableElement.attr('id'));
7179
FileAPIProxy.container
72-
.height(input.outerHeight())
73-
.width(input.outerWidth())
74-
.css(input.offset());
80+
.height(clickableElement.outerHeight())
81+
.width(clickableElement.outerWidth())
82+
.css(clickableElement.offset());
7583
}
7684
})
7785
.click(function(e) {

jquery.FileReader.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)