How to Remove File Extension Dynamically in JavaScript

How to Remove File Extension Dynamically in JavaScript

Introduction

In this tutorial we will create a How to Remove File Extension Dynamically in JavaScript. This tutorial purpose is to teach you on how to remove file extension dynamically. This will cover all the important functionality that will remove the file extension. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application if you want to display a file without extension. I will give my best to provide you the easiest way of creating this program Remove File Extension. So let's do the coding.

Before we get started:

Here is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only display the html list and buttons. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">How to Remove File Extension Dynamically</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <h2>List of File</2>
  17. <br />
  18. <div class="col-md-5">
  19. <br />
  20. <center><button class="btn btn-success" onclick="resetList();">Reset</button></center>
  21. <button class="btn btn-danger" onclick="removeEx('remove');">Remove</button> <button class="btn btn-info" onclick="removeEx('display');">Extension Only</button>
  22. </div>
  23. <div class="col-md-7">
  24. <ol type="I" style="font-size:20px;" id="result"></ol>
  25. </div>
  26. </div>
  27. <script src="script.js"></script>
  28. </body>
  29. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will remove the file extension when the button is clicked. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. var names=["style.css", "index.html", "anime.mp4", "image.jpg", "music.mp3", "funny.gif", "newChar.png"];
  2. let html="";
  3.  
  4. displayList();
  5.  
  6. function displayList(){
  7. html="";
  8. for(var i=0; i<names.length; i++){
  9. html+="<li>"+names[i]+"</li>";
  10. }
  11.  
  12. document.getElementById('result').innerHTML=html;
  13. }
  14.  
  15. function resetList(){
  16. displayList();
  17. }
  18.  
  19. function removeEx(str){
  20. if(str=="remove"){
  21. html="";
  22. for(var i=0; i<names.length; i++){
  23. var list=names[i].split(".");
  24. html+="<li>"+list[0]+"</li>";
  25. }
  26.  
  27. }else if(str=="display"){
  28. html="";
  29. for(var i=0; i<names.length; i++){
  30. var list=names[i].split(".");
  31. html+="<li>"+list[1]+"</li>";
  32. }
  33.  
  34.  
  35. }
  36. document.getElementById('result').innerHTML=html;
  37. }

The code above only create one method to remove the file extension called removeEx(). This function will remove the file extension on the list. This code use a special javascript function called split(), this function will remove any elements base on the parameter you have set.

Output:

The How to Remove File Extension Dynamically in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Remove File Extension Dynamically in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment