Home > Web Front-end > JS Tutorial > How Can I Clear a File Input Control's Value Using jQuery?

How Can I Clear a File Input Control's Value Using jQuery?

Mary-Kate Olsen
Release: 2024-12-13 03:01:16
Original
917 people have browsed it

How Can I Clear a File Input Control's Value Using jQuery?

Clearing File Input Control with jQuery

The question arises: is it feasible to reset the value of an HTML file input control (

To address this issue, a unique solution emerges. The file input control is wrapped within a form element. Subsequently, the reset method is invoked on the form, effectively clearing the input values. Finally, the form is removed using the unwrap() function.

Crucially, this technique leaves the original file input control intact, preserving any custom properties or attributes. Tests have confirmed its compatibility with a range of browsers, including Opera, Firefox, Safari, Chrome, and IE6 .

For added versatility, this approach can be applied to other form elements as well. However, hidden input fields remain an exception.

Below is a code snippet that showcases how to implement this solution:

window.reset = function(e) {
  e.wrap('<form>').closest('form').get(0).reset();
  e.unwrap();
};</p>
<pre class="brush:php;toolbar:false"><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<form>
  <input>
Copy after login

The above is the detailed content of How Can I Clear a File Input Control's Value Using jQuery?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template