Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MrProgramerShah/472816ecd17824e3b2302cb2cd750f81 to your computer and use it in GitHub Desktop.
Save MrProgramerShah/472816ecd17824e3b2302cb2cd750f81 to your computer and use it in GitHub Desktop.
Custom File Upload Button | HTML + CSS + JavaScript
<input type="file" id="real-file" hidden="hidden" />
<button type="button" id="custom-button">CHOOSE A FILE</button>
<span id="custom-text">No file chosen, yet.</span>
const realFileBtn = document.getElementById("real-file");
const customBtn = document.getElementById("custom-button");
const customTxt = document.getElementById("custom-text");
customBtn.addEventListener("click", function() {
realFileBtn.click();
});
realFileBtn.addEventListener("change", function() {
if (realFileBtn.value) {
customTxt.innerHTML = realFileBtn.value.match(
/[\/\\]([\w\d\s\.\-\(\)]+)$/
)[1];
} else {
customTxt.innerHTML = "No file chosen, yet.";
}
});
#custom-button {
padding: 10px;
color: white;
background-color: #009578;
border: 1px solid #000;
border-radius: 5px;
cursor: pointer;
}
#custom-button:hover {
background-color: #00b28f;
}
#custom-text {
margin-left: 10px;
font-family: sans-serif;
color: #aaa;
}
@GorgeousOne
Copy link

Nice, thx, exactly what I was looking for, good tutorial

@deshna-s
Copy link

instead of the file name, I want to display a selected image
so, how am I supposed to do that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment