Created
March 8, 2022 05:25
-
-
Save w3collective/7b95bd34bee030b0ffdcdf26c5d93185 to your computer and use it in GitHub Desktop.
How to get file extensions using JavaScript
This file contains 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>Get file extension using JavaScript</title> | |
<link rel="stylesheet" href="style.css" /> | |
</head> | |
<body> | |
<form> | |
<fieldset> | |
<legend>Check file extension:</legend> | |
<input type="file" id="input-file-upload" /> | |
<input type="button" id="btn-check-extension" value="Check Extension" /> | |
</fieldset> | |
</form> | |
<p>File extension: <span id="result"></span></p> | |
<script> | |
const btn = document.querySelector("#btn-check-extension"); | |
btn.addEventListener('click', () => getExtension()); | |
const getExtension = () => { | |
const file = document.querySelector("#input-file-upload").value; | |
const extension = file.split(".").pop(); | |
document.querySelector("#result").textContent = extension; | |
}; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source => https://w3collective.com/file-extension-javascript/