• We just launched and are currently in beta. Join us as we build and grow the community.

How to Remove File Extension Dynamically in JavaScript

Harvey

Infrastructure Monitoring Expert
H Rep
0
0
0
Rep
0
H Vouches
0
0
0
Vouches
0
Posts
59
Likes
75
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
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. <body

    >
  8. <nav

    class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav

    >
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    "text-primary"

    >

    How to Remove File Extension Dynamically</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  17. <h2

    >

    List of File</

    2

    >
  18. <br

    /

    >
  19. <div

    class

    =

    "col-md-5"

    >
  20. <br

    /

    >
  21. <center><button

    class

    =

    "btn btn-success"

    onclick

    =

    "resetList();"

    >

    Reset</

    button

    ></

    center>
  22. <button

    class

    =

    "btn btn-danger"

    onclick

    =

    "removeEx('remove');"

    >

    Remove</

    button

    >

    <button

    class

    =

    "btn btn-info"

    onclick

    =

    "removeEx('display');"

    >

    Extension Only</

    button

    >
  23. </

    div

    >
  24. <div

    class

    =

    "col-md-7"

    >
  25. <ol

    type

    =

    "I"

    style

    =

    "font-size:20px;"

    id

    =

    "result"

    ></

    ol

    >
  26. </

    div

    >
  27. </

    div

    >
  28. <script

    src

    =

    "script.js"

    ></

    script

    >
  29. </

    body

    >
  30. </

    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. displayList(

    )

    ;

  4. function

    displayList(

    )

    {
  5. html=

    ""

    ;
  6. for

    (

    var

    i=

    0

    ;

    i<

    names.length

    ;

    i++

    )

    {
  7. html+=

    "<li>"

    +

    names[

    i]

    +

    "</li>"

    ;
  8. }

  9. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    html;
  10. }

  11. function

    resetList(

    )

    {
  12. displayList(

    )

    ;
  13. }

  14. function

    removeEx(

    str)

    {
  15. if

    (

    str==

    "remove"

    )

    {
  16. html=

    ""

    ;
  17. for

    (

    var

    i=

    0

    ;

    i<

    names.length

    ;

    i++

    )

    {
  18. var

    list=

    names[

    i]

    .split

    (

    "."

    )

    ;
  19. html+=

    "<li>"

    +

    list[

    0

    ]

    +

    "</li>"

    ;
  20. }

  21. }

    else

    if

    (

    str==

    "display"

    )

    {
  22. html=

    ""

    ;
  23. for

    (

    var

    i=

    0

    ;

    i<

    names.length

    ;

    i++

    )

    {
  24. var

    list=

    names[

    i]

    .split

    (

    "."

    )

    ;
  25. html+=

    "<li>"

    +

    list[

    1

    ]

    +

    "</li>"

    ;
  26. }


  27. }
  28. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    html;
  29. }

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:

How%20to%20Remove%20File%20Extension%20Dynamically%20in%20JavaScript%201.png


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


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

330,760

330,768

Top