-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
47 lines (38 loc) · 1.8 KB
/
javascript.js
File metadata and controls
47 lines (38 loc) · 1.8 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
43
44
45
46
47
buttonifyInputs = function(buttonClasses = '', lableClasses = '') {
const labelEmptyFileListText = "No files selected";
const buttonText = "Select file";
document.addEventListener("DOMContentLoaded", function() {
document.querySelectorAll("input[type=file]").forEach((input, index) => {
// check if the input is already "button-ified"
if (input.dataset.buttonIfied) {
return
}
id = input.id !== "" ? input.id : `file-${index}`
// create the label element
const label = document.createElement("label");
label.className = lableClasses;
label.textContent = labelEmptyFileListText;
label.setAttribute("for", id);
// create the button element
const button = document.createElement("button");
button.className = buttonClasses;
button.textContent = buttonText
button.onclick = () => input.click();
// hide the original input element
input.style.display = "none"
// set id incase it was not set before
input.id = id
// append elements after the hidden input
input.insertAdjacentElement('afterend', button)
.insertAdjacentElement('afterend', document.createElement("br"))
.insertAdjacentElement('afterend', label)
// set the label when input value changes
input.onchange = (event) => {
const fileNames = Array.from(event.target.files).map((file) => file.name).join(", ")
label.textContent = fileNames !== "" ? fileNames : labelEmptyFileListText
}
// mark input element as "button-ified"
input.dataset.buttonIfied = true
})
});
}