Skip to content Skip to sidebar Skip to footer

Javascript: True Form Reset For Hidden Fields

Unfortunately form.reset() function doesn't reset hidden inputs of the form. Checked in FF3 and Chromium. Does any one have an idea how to do the reset for hidden fields as well?

Solution 1:

Seems the easiest way of doing that is having display: none text field instead of hidden field. At this case default reset process regularly.

Solution 2:

This is correct as per the standard, unfortunately. A bad spec wart IMO. IE provides hidden fields with a resettable defaultValue nonetheless. See this discussion: it's not (alas) going to change in HTML5.

(Luckily, there is rarely any need to reset a form. As a UI feature it's generally frowned upon.)

Since you can't get the original value of the value attribute at all, you would have to duplicate it in another attribute and fetch that. eg.:

<form id="f">

<inputtype="hidden"name="foo"value="bar"class="value=bar"/>functionresetForm() {
    var f= document.getElementById('f');
    f.reset();
    f.elements.foo.value= Element_getClassValue(f.elements.foo, 'value');
}

functionElement_getClassValue(el, classname) {
    var prefix= classname+'=';
    var classes= el.className.split(/\s+/);
    for (var i= classes.length; i-->0;)
        if (classes[i].substring(0, prefix.length)===prefix)
            return classes[i].substring(prefix.length);
    return'';
}

Alternative ways of smuggling that value in might include HTML5 data, another spare attribute like title, an immediately-following <!-- comment --> to read the value from, explicit additional JS information, or extra hidden fields just to hold the default values.

Whatever approach, it would have to clutter up the HTML; it can't be created by script at document ready time because some browsers will have already overridden the field's value with a remembered value (from a reload or back button press) by that time that code executes.

Solution 3:

Another answer, in case anyone comes here looking for one. Serialize the form after the page loads and use those values to reset the hidden fields later:

var serializedForm = $('#myForm').serialize();

Then, to reset the form:

functionfullReset(){

    $('#myForm').reset(); // resets everything except hidden fieldsvar formFields = decodeURIComponent(serializedForm).split('&'); //split up the serialized form into variable pairs//put it into an associative arrayvar splitFields = newArray();
    for(i in formFields){
        vals= formFields[i].split('=');
        splitFields[vals[0]] = vals[1];
    }
    $('#myForm').find('input[type=hidden]').each(function(){    
        this.value = splitFields[this.name];
    });

}

Solution 4:

I found it easier to just set a default value when the document is loaded then trap the reset and reset the hidden puppies back to their original value. For example,

//fix form reset (hidden fields don't get reset - this will fix that pain in the arse issue)
$( document ).ready(function() {
  $("#myForm").find("input:hidden").each(function() {
      $(this).data("myDefaultValue", $(this).val());
  });

  $("#myForm").off("reset.myarse");
  $("#myForm").on("reset.myarse", function() {
     var myDefaultValue = $(this).data("myDefaultValue");
     if (myDefaultValue != null) {
       $(this).val(myDefaultValue);
     }
  });
}

Hope this helps someone out :)

Solution 5:

You can use jQuery - this will empty hidden fields:

$('form').on('reset', function() {
  $("input[type='hidden']", $(this)).val('');
});

Tip: just make sure you're not resetting csrf token field or anything else that shouldn't be emptied. You can narrow down element's specification if needed.

If you want to reset the field to a default value you can use(not tested):

$('form').on('reset', function() {
  $("input[type='hidden']", $(this)).each(function() {
    var $t = $(this);
    $t.val($t.data('defaultvalue'));
  });
});

and save the default value in the data-defaultvalue="Something" property.

Post a Comment for "Javascript: True Form Reset For Hidden Fields"