views:

524

answers:

5

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?

A: 

Using jQuery you could do this:

jQuery(':hidden').val('');
Paddy
`val()` is a getter and doesn't always work. So better supply an empty string to clear the field.
Aaron Digulla
Same here. Reset is not equal clear.
Bogdan Gusiev
Very true, sorry... Just getting my morning coffee.
Paddy
Oh, sorry, you want them back to their original values. I'd guess you'll have to have a set of fields that contain those originals to set them to...
Paddy
A: 

Create a button and add JavaScript to the onClick event which clears the fields.

That said, I'm curious why you want to reset these fields. Usually, they contain internal data. If I would clear them in my code, the post of the form would fail (for example after the user has entered the new data and tries to submit the form).

[EDIT] I misunderstood your question. If you're worried that someone might tamper with the values in the hidden fields, then there is no way to reset them. For example, you can call reset() on the form but not on a field in the form.

You could think that you could save the values in a JavaScript file and use that to reset the values but when a user can tamper with the hidden fields, he can tamper with the JavaScript as well.

So from a security point of view, if you need to reset hidden fields, then avoid them in the first place and save the information in the session on the server.

Aaron Digulla
reset is not equal clear. Reset means set to initial state.
Bogdan Gusiev
Ah, I see. See my edits.
Aaron Digulla
A: 

How I would do it is put an event listener on the change event of the hidden field. In that listener function you could save the initial value to the DOM element storage (mootools, jquery) and then listen to the reset event of the form to restore the initial values stored in the hidden form field storage.

ChrisR
A: 

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

Bogdan Gusiev
A: 

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">

<input type="hidden" name="foo" value="bar" class="value=bar"/>

function resetForm() {
    var f= document.getElementById('f');
    f.reset();
    f.elements.foo.value= Element_getClassValue(f.elements.foo, 'value');
}

function Element_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.

bobince