JavaScript - Remove Extension Name

In this tutorial we will create a Remove Extension Name using JavaScript. This code will dynamically remove the file extension when user click the button. The code itself use onlick() to call a function that will dynamically remove the file extension using dot notation split with a set parameter. This code is free to use you can modify it as your own, We will be using JavaScript as a server-side scripting language because It allows greater control of your web page behavior than HTML alone. It is embedded in HTML that responsible to allow user to interact with the computer .

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this 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">JavaScript - Remove Extension Name</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-5">
  17. <h3>List of File Formats</h3>
  18. <center><button class="btn btn-primary" onclick="removeEx('remove');">Remove Extension</button></center>
  19. <br />
  20. <center><button class="btn btn-default" onclick="removeEx('display');">Display Extension</button></center>
  21. <br /><br />
  22. <ul style="font-size:20px;" id="result"></ul>
  23. </div>
  24. </div>
  25. <script src="js/script.js"></script>
  26. </body>
  27. </html>

Creating the Script

This code contains the script of the application. This code will dynamically remove the file extension when the inputs is entered. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. var names=["jack.mp4", "image.jpg", "music.mp3", "play.gif", "newChar.png"];
  2. var 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 removeEx(str){
  16. if(str=="remove"){
  17. html="";
  18. for(var i=0; i<names.length; i++){
  19. var list=names[i].split(".");
  20. html+="<li>"+list[0]+"</li>";
  21. }
  22.  
  23. }else if(str=="display"){
  24. html="";
  25. for(var i=0; i<names.length; i++){
  26. var list=names[i].split(".");
  27. html+="<li>"+list[1]+"</li>";
  28. }
  29.  
  30.  
  31. }
  32. document.getElementById('result').innerHTML=html;
  33. }
There you have it we successfully created a Remove Extension Name using 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!

Add new comment