Repository navigation
Expand file tree
/
Copy pathjquery.js
More file actions
42 lines (37 loc) · 1.56 KB
/
Copy pathjquery.js
File metadata and controls
42 lines (37 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
buttonifyInputs = function(buttonClasses = '', lableClasses = '') {
const labelEmptyFileListText = "No files selected";
const buttonText = "Select file";
$(document).ready(function() {
$("input[type=file]").each((index, element) => {
// check if the input is already "button-ified"
if ($(element).data('buttonIfied')) {
return;
}
const id = element.id !== "" ? element.id : `file-${index}`
// create the button element
const button = $("<button />")
.text(buttonText)
.on("click", () => element.click())
.addClass(buttonClasses)
// create the label element
const label = $("<label />")
.text(labelEmptyFileListText)
.attr("for", id)
.addClass(lableClasses)
$(element)
// hide the original input element
.hide()
// set id incase it was not set before
.attr('id', id)
// append the new elements
.after(button, "<br />", label)
// set the label when input value changes
.on('change', (event) => {
const fileNames = Array.from(event.target.files).map((file) => file.name).join(", ")
label.text(fileNames !== "" ? fileNames : labelEmptyFileListText);
})
// mark input element as "button-ified"
.data('buttonIfied', true);
});
});
}