This is code for a basic custom file upload button in HTML, CSS and JavaScript. See the full video tutorial here: https://youtu.be/T3PDgtliezo
Created
November 1, 2019 09:13
-
-
Save MrProgramerShah/472816ecd17824e3b2302cb2cd750f81 to your computer and use it in GitHub Desktop.
Custom File Upload Button | HTML + CSS + JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."; | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
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
Nice, thx, exactly what I was looking for, good tutorial