{"id":4896,"date":"2023-01-26T18:17:27","date_gmt":"2023-01-26T17:17:27","guid":{"rendered":"https:\/\/ekiwi.de\/?p=4896"},"modified":"2023-01-26T18:18:48","modified_gmt":"2023-01-26T17:18:48","slug":"empty-file-upload-field-in-html-form","status":"publish","type":"post","link":"https:\/\/ekiwi.de\/en\/index.php\/4896\/empty-file-upload-field-in-html-form\/","title":{"rendered":"Empty file upload field in HTML form"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of content<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ekiwi.de\/en\/index.php\/4896\/empty-file-upload-field-in-html-form\/#Manual_options_for_the_user\" >Manual options for the user<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ekiwi.de\/en\/index.php\/4896\/empty-file-upload-field-in-html-form\/#Solution_with_Java_Script\" >Solution with Java Script<\/a><\/li><\/ul><\/nav><\/div>\n<p>How can a file be removed from the upload field in HTML by the user? Two solutions.<\/p>\n<p><!--more--><\/p>\n<p>When the user selects a file in a <a href=\"https:\/\/da-software.net\/software\/da-formmaker\/\" target=\"_blank\" rel=\"noopener\">file upload field<\/a>, it is selected for now. But how can you reset the form field? If the wrong file is selected.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Manual_options_for_the_user\"><\/span>Manual options for the user<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/wp-content\/uploads\/2023\/01\/upload_1.png\" alt=\"\" width=\"381\" height=\"108\" class=\"alignnone size-full wp-image-4890\" srcset=\"\/wp-content\/uploads\/2023\/01\/upload_1.png 381w, \/wp-content\/uploads\/2023\/01\/upload_1-300x85.png 300w\" sizes=\"auto, (max-width: 381px) 100vw, 381px\" \/><\/p>\n<p>The user has two options here, the first is the <a href=\"http:\/\/html.ekiwi.de\/html\/formulare\/formularbuttons.htm\" target=\"_blank\" rel=\"noopener\">Reset or Cancel field<\/a>. If this is present in the form, the form can be reset here. However, this has the side effect that all other entries are also removed.<\/p>\n<p>As a user, there is a second possibility. For this we select a file again, but click on &#8220;Cancel&#8221; in the file dialogue. This also removes the file selection. Not intuitive, but once you know&#8230;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/wp-content\/uploads\/2023\/01\/upload_2.png\" alt=\"\" width=\"292\" height=\"90\" class=\"alignnone size-full wp-image-4892\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Solution_with_Java_Script\"><\/span>Solution with Java Script<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The second solution has to be implemented by the creator of the form. For this we use a button and Java script (<a href=\"https:\/\/stackoverflow.com\/questions\/829571\/clearing-an-html-file-upload-field-via-javascript\" target=\"_blank\" rel=\"noopener\">source Stackoverflow<\/a>).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/wp-content\/uploads\/2023\/01\/upload_3.png\" alt=\"\" width=\"486\" height=\"120\" class=\"alignnone size-full wp-image-4894\" srcset=\"\/wp-content\/uploads\/2023\/01\/upload_3.png 486w, \/wp-content\/uploads\/2023\/01\/upload_3-300x74.png 300w, \/wp-content\/uploads\/2023\/01\/upload_3-480x120.png 480w\" sizes=\"auto, (max-width: 486px) 100vw, 486px\" \/><\/p>\n<p>We create the button in HTML within the form:<\/p>\n<pre>&lt;button id=&quot;ClearUpload&quot; type=&quot;button&quot;&gt;Clear&lt;\/button&gt; <\/pre>\n<p>Then we put react when the user clicks on the button.<\/p>\n<pre>$(document).ready(function () {\r\n    $('#ClearUpload').click(function () {\r\n        clearFileInput('Upload');\r\n    });\r\n});<\/pre>\n<p>The actual resetting is then done by the following function, here we pass the ID of the upload field.<\/p>\n<pre>function clearFileInput(id) {}\r\n    var oldInput = document.getElementById(id);\r\n    var newInput = document.createElement(&quot;input&quot;);\r\n    newInput.type = &quot;file&quot;;\r\n    newInput.id = oldInput.id;\r\n    newInput.name = oldInput.name;\r\n    newInput.className = oldInput.className;\r\n    newInput.style.cssText = oldInput.style.cssText;\r\n    oldInput.parentNode.replaceChild(newInput, oldInput);\r\n}<\/pre>\n<p>The finished example is available <a href=\"https:\/\/ekiwi.de\/tryit\/?id=241\" target=\"_blank\" rel=\"noopener\">here for testing<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>How can a file be removed from the upload field in HTML by the user? Two solutions.<\/p>\n","protected":false},"author":1,"featured_media":4264,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[881,870],"tags":[905],"class_list":["post-4896","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript-en","category-programming","tag-html-en"],"_links":{"self":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/posts\/4896","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/comments?post=4896"}],"version-history":[{"count":0,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/posts\/4896\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/media\/4264"}],"wp:attachment":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/media?parent=4896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/categories?post=4896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/tags?post=4896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}