Skip to content

Instantly share code, notes, and snippets.

@mtsmfm
Created November 15, 2024 10:05
Show Gist options
  • Save mtsmfm/fa27a9819cab93a5fefc7590ecc4a151 to your computer and use it in GitHub Desktop.
Save mtsmfm/fa27a9819cab93a5fefc7590ecc4a151 to your computer and use it in GitHub Desktop.
file upload with elem.click
<html>
<head>
<title>File upload</title>
</head>
<body>
<input id="file" type="file" hidden onchange="onFileAttached()" />
<button id="button" onclick="uploadFile()">Upload</button>
<div id="filename"></div>
</body>
<script>
const button = document.getElementById("button")
button.onclick = () => {
const fileElem = document.getElementById("file")
fileElem.click()
}
const file = document.getElementById("file")
file.onchange = (event) => {
const file = event.target.files[0]
const filename = document.getElementById("filename")
filename.innerText = file.name
}
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment