Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@w3collective
Created March 8, 2022 05:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save w3collective/7b95bd34bee030b0ffdcdf26c5d93185 to your computer and use it in GitHub Desktop.
Save w3collective/7b95bd34bee030b0ffdcdf26c5d93185 to your computer and use it in GitHub Desktop.
How to get file extensions using JavaScript
<!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>
@w3collective
Copy link
Author

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