I have two upload button (Upload Photo) & (Upload Signature). The issue is on same time only one button is uploading file second button is not uploading file, when I change the input ID on that time both buttons not working, I have placed both html code and script code and I want uploaded file name with path, Please fix the issue.
JavaScript
x
40
40
1
// This script is for photo upload button
2
3
const realFileBtn = document.getElementById("real-file-upload");
4
const customBtn = document.getElementById("custom-button");
5
const customTxt = document.getElementById("custom-text");
6
7
customBtn.addEventListener("click", function() {
8
realFileBtn.click();
9
});
10
11
realFileBtn.addEventListener("change", function() {
12
if (realFileBtn.value) {
13
customTxt.innerHTML = realFileBtn.value.match(
14
/[/\]([wds.-()]+)$/
15
)[1];
16
} else {
17
customTxt.innerHTML = "No file chosen, yet.";
18
}
19
});
20
21
22
// This script is for signature upload button
23
24
const realFileBtn = document.getElementById("real-file-upload");
25
const customBtn = document.getElementById("custom-button");
26
const customTxt = document.getElementById("custom-text");
27
28
customBtn.addEventListener("click", function() {
29
realFileBtn.click();
30
});
31
32
realFileBtn.addEventListener("change", function() {
33
if (realFileBtn.value) {
34
customTxt.innerHTML = realFileBtn.value.match(
35
/[/\]([wds.-()]+)$/
36
)[1];
37
} else {
38
customTxt.innerHTML = "No file chosen, yet.";
39
}
40
});
JavaScript
1
11
11
1
<div class="col-md-4">
2
<input type="file" id="real-file-upload" hidden="hidden" />
3
<button type="button" id="custom-button">Upload Photo</button>
4
<span id="custom-text">No file chosen, yet.</span>
5
</div>
6
7
<div class="col-md-4">
8
<input type="file" id="real-file-signature" hidden="hidden" />
9
<button type="button" id="custom-button">Upload Signature</button>
10
<span id="custom-text">No file chosen, yet.</span>
11
</div>
Advertisement
Answer
You need to delegate
- I changed id to class everywhere
- I wrapped the two sets in one container
JavaScript
1
19
19
1
const container = document.getElementById("container")
2
container.addEventListener("click", function(e) {
3
const tgt = e.target;
4
if (tgt.classList.contains("custom-button")) tgt.closest("div").querySelector(".file-upload").click()
5
});
6
7
container.addEventListener("change", function(e) {
8
const tgt = e.target;
9
const parent = tgt.closest("div")
10
const realFileBtn = parent.querySelector(".file-upload")
11
const customTxt = parent.querySelector(".custom-text")
12
if (realFileBtn.value) {
13
customTxt.innerHTML = realFileBtn.value.match(
14
/[/\]([wds.-()]+)$/
15
)[1];
16
} else {
17
customTxt.innerHTML = "No file chosen, yet.";
18
}
19
});
JavaScript
1
18
18
1
.custom-button {
2
padding: 10px;
3
color: white;
4
background-color: #009297;
5
border: 1px solid #009297;
6
border-radius: 5px;
7
cursor: pointer;
8
}
9
10
.custom-button:hover {
11
background-color: #00b28f;
12
}
13
14
.custom-text {
15
margin-left: 10px;
16
font-family: sans-serif;
17
color: #aaa;
18
}
JavaScript
1
13
13
1
<div id="container">
2
<div class="col-md-4">
3
<input type="file" class="file-upload" hidden="hidden" />
4
<button type="button" class="custom-button">Upload Photo</button>
5
<span class="custom-text">No file chosen, yet.</span>
6
</div>
7
8
<div class="col-md-4">
9
<input type="file" class="file-upload" hidden="hidden" />
10
<button type="button" class="custom-button">Upload Signature</button>
11
<span class="custom-text">No file chosen, yet.</span>
12
</div>
13
</div>