JavaScript implements a method of emptying (resetting) a file type INPUT element value _javascript tips

Source: Internet
Author: User
Tags event listener

The examples in this article describe the way JavaScript implements empty (reset) file type INPUT element values. Share to everyone for your reference, specific as follows:

Because of security restrictions, scripts cannot set their value values arbitrarily, so they cannot be set to reset with attributes as other form input fields do.

Resetting the value of a file field can be summed up in 3 main ways.

This article analyzes the browser compatibility and advantages and disadvantages of these three methods, and gives a more perfect combination of code and demo.

Three ways to reset a file field:

1. Set the Value property to null.

For IE11 above and other newer non IE modern browser chrome/firefox/opera ... Effective.

2. Clone or create a new file INPUT element to replace it.

Use createelement or CloneNode clones or create a new element to replace it for all browsers. The disadvantage is also obvious, that is, the replacement will lose the previously bound event listener and lose some of the custom expando properties. Without this problem can be used, not generic, I do not recommend the use of this method.

3. Call the Reset () method of the form form element.

The reset () method of the form element resets all the input elements within the form, which is not what we expect, so you can work around creating a new form object, putting the file INPUT element in and then resetting it, and then removing the file INPUT element and putting it back in place. This will not show the drawbacks of Method 2.

Combined with Method 1 and Method 3, all browsers can be compatible.

The JavaScript function code is as follows:

function Clearinputfile (f) {
  if (f.value) {
    try{
      f.value = ';//for IE11, Latest Chrome/firefox/opera ...
    }catch (Err) {
    }
    if (f.value) {//for IE5 ~ IE10
      var form = document.createelement (' form '), ref = f.nextsibling, p = f.parentnode;
      Form.appendchild (f);
      Form.reset ();
      P.insertbefore (F,REF);}}


More readers interested in JavaScript-related content can view the site topics: "JavaScript Search Algorithm Skills Summary", "JavaScript data structure and algorithm skills summary", "JavaScript traversal algorithm and Skills summary", " A summary of JSON manipulation techniques in JavaScript, a summary of JavaScript switching effects and techniques, a summary of JavaScript animation effects and techniques, JavaScript errors and debugging tips, and a summary of JavaScript mathematical operational usage

I hope this article will help you with JavaScript programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.