{"id":8450,"date":"2023-08-01T07:09:24","date_gmt":"2023-08-01T07:09:24","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=8450"},"modified":"2023-08-01T07:09:24","modified_gmt":"2023-08-01T07:09:24","slug":"target-filename-javascript","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/javascript\/target-filename-javascript","title":{"rendered":"How to Get the fileName in JavaScript from Input File Tag in IE Browser"},"content":{"rendered":"<p>Here&#8217;s an example how you can get file-name from file input in Javascript:<\/p>\n<p><strong>Code of index.html:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n&lt;head&gt;\r\n    &lt;meta charset=\"UTF-8\"&gt;\r\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\r\n    &lt;title&gt;Document&lt;\/title&gt;\r\n    &lt;script src=\".\/script.js\"&gt;&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n    &lt;div&gt;\r\n        &lt;label for=\"file\"&gt;Select file&lt;\/label&gt;\r\n        &lt;input type=\"file\" name=\"file\" id=\"file\"&gt;\r\n    &lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p><strong>Code of script.js:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">window.onload = function() {\r\n    document.getElementById('file').addEventListener('change', getFileName);\r\n}\r\n\r\nconst getFileName = (event) =&gt; {\r\n    const files = event.target.files;\r\n    const fileName = files[0].name;\r\n    console.log(\"file name: \", fileName);\r\n}\r\n<\/pre>\n<p><strong>Explanation:<\/strong><\/p>\n<p>In the above code, We are adding an event listener to the input element which detects the changes and executes the getFileName method.<br \/>\nIn the getFileName method, using the event object of javascript, we can get selected file objects. If we want to get file-name then we can use the name property of the selected file object.<\/p>\n<p>Here\u2019s an example for multiple file inputs in single html file,<\/p>\n<p><strong>Code of index.html:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n&lt;head&gt;\r\n    &lt;meta charset=\"UTF-8\"&gt;\r\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\r\n    &lt;title&gt;Document&lt;\/title&gt;\r\n    &lt;script src=\".\/script.js\"&gt;&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n    &lt;div&gt;\r\n        &lt;label for=\"file\"&gt;Select file 1:&lt;\/label&gt;\r\n        &lt;input type=\"file\" name=\"file\" class=\"file\"&gt;\r\n    &lt;\/div&gt;\r\n    &lt;div&gt;\r\n        &lt;label for=\"file\"&gt;Select file 2:&lt;\/label&gt;\r\n        &lt;input type=\"file\" name=\"file\" class=\"file\"&gt;\r\n    &lt;\/div&gt;\r\n    &lt;div&gt;\r\n        &lt;label for=\"file\"&gt;Select file 3:&lt;\/label&gt;\r\n        &lt;input type=\"file\" name=\"file\" class=\"file\"&gt;\r\n    &lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p><strong>Code of script.js:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">window.onload = function() {\r\n    const fileCollection = document.getElementsByClassName('file');\r\n    for(let fileInput of fileCollection) {\r\n        fileInput.addEventListener('change', getFileName);\r\n    }\r\n}\r\n\r\nconst getFileName = (event) =&gt; {\r\n    const files = event.target.files;\r\n    const fileName = files[0].name;\r\n    console.log(\"file name: \", fileName);\r\n}\r\n<\/pre>\n<p>Now for multiple file inputs, We just have to use class selector instead of id selector and iterate the collection returned by the getElementsByClassName() method.<br \/>\nSimilar to a single file logic, for each input element, add the event listener which detects the changes and we will get file name in the getFileName method.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s an example how you can get file-name from file input in Javascript: Code of index.html: &lt;!DOCTYPE html&gt; &lt;html lang=&#8221;en&#8221;&gt; &lt;head&gt; &lt;meta charset=&#8221;UTF-8&#8243;&gt; &lt;meta name=&#8221;viewport&#8221; content=&#8221;width=device-width, initial-scale=1.0&#8243;&gt; &lt;title&gt;Document&lt;\/title&gt; &lt;script src=&#8221;.\/script.js&#8221;&gt;&lt;\/script&gt; &lt;\/head&gt; &lt;body&gt; &lt;div&gt; &lt;label for=&#8221;file&#8221;&gt;Select file&lt;\/label&gt; &lt;input type=&#8221;file&#8221; name=&#8221;file&#8221; id=&#8221;file&#8221;&gt; &lt;\/div&gt; &lt;\/body&gt; &lt;\/html&gt; Code of script.js: window.onload = function() { document.getElementById(&#8216;file&#8217;).addEventListener(&#8216;change&#8217;, getFileName); } const getFileName [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8451,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[22],"tags":[],"class_list":["post-8450","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8450"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=8450"}],"version-history":[{"count":2,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8450\/revisions"}],"predecessor-version":[{"id":8453,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8450\/revisions\/8453"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/8451"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=8450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=8450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=8450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}