MozillaZine

Firefox will not reset hidden fields

Discuss how to use and promote Web standards with the Mozilla Gecko engine.
Alwin
 
Posts: 3
Joined: March 8th, 2010, 4:41 am

Post Posted March 8th, 2010, 5:26 am

Hi Experts,

In firefox hidden fields do not reset using javascript.

In my example (see code) there is a button to reset the value of the hidden field and one button to reset the entiere form.
And there is a button to change the value of the hidden field.
In Firefox none of the reset buttons have any effect on the hidden field value.
The defaultValue property does not function with hidden fields.
See also http://webradiall.applixia.net/test.htm

Any Ideas?

Thanks

Code: Select all
<form name="myForm">
   <input type="text" name="myText" size="20" value="startvalue"><p>
   <input type="button" value="set hidden field to 5" name="B2" onclick="this.form.hiddenField.value='5'"></p>
   <p>
   <input type="reset" value="RESET entire Form" name="B1"> </p>
   <p>
   <input type="button" value="Reset only hidden field" name="B4" onclick="this.form.hiddenField.value=this.form.hiddenField.defaultValue">
   </p>
   <p>
   <input type="button" value="Reset only text field" name="B5" onclick="this.form.myText.value=this.form.myText.defaultValue"></p>
   <p>
   &nbsp;</p>
   <p>
   &nbsp;<input type="button" value="show hidden field value" name="B3" onclick="alert(this.form.hiddenField.value)">
   <input type="button" value="Show hidden field defaultValue" name="B7" onclick="alert(this.form.hiddenField.defaultValue)">
   <input type="button" value="Show text field defaultValue" name="B8" onclick="alert(this.form.myText.defaultValue)"></p>
   <input type="hidden" name="hiddenField" value="25">
</form>



Mordwin
 
Posts: 653
Joined: June 8th, 2005, 6:10 am

Post Posted March 8th, 2010, 6:00 am

defaultValue of type DOMString
When the type attribute of the element has the value "text", "file" or "password", this represents the HTML value attribute of the element. The value of this attribute does not change if the contents of the corresponding form control, in an interactive user agent, changes. See the value attribute definition in HTML 4.01.


So it would seem that hidden Input elements do not have a defaultValue, though Firefox seems to be returning the vale property rather than complaining.

Further reading suggest that for hidden Input elements, the value property is the same as the value attribute of the source HTML (this is unlike for say text Input elements where the defaultValue property is the value attribute, and the value property is separate, though initially has the same contents). So for hidden Input elements, treating defaultValue and value as the same is probably reasonable, as they in fact are the same).

So, to do what you want, you'll need to capture the hidden element's value on form load into a variable, and use that to reset the value property (and avoid using the defaultValue property altogether).

Alwin
 
Posts: 3
Joined: March 8th, 2010, 4:41 am

Post Posted March 8th, 2010, 6:28 am

Hi Mordwin,

Thanks for your reaction.
Resetting a form containg hidden input fields in Firefox has no use, for the hidden fields that is.
Wouldn't you say that's a bug in Firefox?
Are you acquainted with scripts that reset all hidden fields to their default value?

Regards,
Alwin

Pim

User avatar
 
Posts: 2205
Joined: May 17th, 2004, 2:04 pm
Location: Netherlands

Post Posted March 8th, 2010, 7:39 am

I believe the philosophy is that resetting an input means to restore its value to the value it had before the user changed it. But hidden inputs can't be changed by the user, so they don't need to be restored. Something along those lines.

If you want to keep the initial values, you might have to fetch those values when the page loads and store them somewhere else (i.e in persistent Javascript variables). Then on resetting, put them back from those variables.
Groetjes, Pim

Alwin
 
Posts: 3
Joined: March 8th, 2010, 4:41 am

Post Posted March 9th, 2010, 3:06 am

Dag Pim,

Dank voor je reactie.
Hidden inputs can't be changed by users, but in my application I use hidden inputs that store info concerning the actions the user takes.
It's a rather complicated application:
http://webradiall.applixia.net/assemblybuilder/
Some values are being retrieved from dropdown inputs in Iframes, so those choices have to be stored in the main form.

I made the following script:
var myElements=new Array();
function saveForm(){ // te gebruiken bij body onload
theForm = document.forms[0]
var flength=theForm.elements.length
var i=0;
for (i=0;i<=(flength-1);i++)
{
myElements[i]=theForm.elements[i].value
}
}
function resetAll(){ // te gebruiken bij de form definition: onreset="resetAll()"
theForm = document.forms[0]
var flength=theForm.elements.length
var i=0;
for (i=0;i<=(flength-1);i++)
{
theForm.elements[i].value=myElements[i]
}
}
Th kind of input types being reset is sufficient for my purpose.
Of course it's possible to insert a type="hidden" condition

I still do't understand why Firefox does not reset hidden fields when the form is being reset.

Groet,
Alwin

Mordwin
 
Posts: 653
Joined: June 8th, 2005, 6:10 am

Post Posted March 9th, 2010, 8:45 am

Because their value and defaultValue are the same thing according to the spec, changing one in code changes both (it literally changes the generated HTML). So resetting a hidden field is in effect a non-operation. This may not be what you expect, but it is basically what the spec says as it's written, so Firefox is doing nothing wrong.

Use a normal text Input, and just give it a display:none style should do what you want with minimum changes?

Cris Mooney
 
Posts: 3
Joined: May 13th, 2004, 2:23 pm

Post Posted February 27th, 2013, 2:24 pm

A bit old, but still relevant reference, so... take care that "text" and "hidden" inputs are not exactly equivalent in functionality. Specifically, "text" inputs may not accept all the values that hidden ones may, and this may bite you if you are assigning unique (special/non-ascii/unprintable) values using Javascript.

Example: textInput.value = 'x'+String.fromCharCode(2)+'y'; // value just gets 'x' in Chrome (it truncates)

Normally this is not a problem, as most do not assign special characters. Moreover, various interference by browsers, and likely server side decodes, messing with CHR(0), CHR(10), and CHR(13) inconsistently, has made more aware of this issue already (IE even messes with these on assignment to hidden fields). However, since all other characters are handled consistently between text and hidden fields on both IE and Firefox, one may be tempted creatively use other special chars. Than along comes Chrome/Safari to get you.

My point: replacement of "hidden" input with "text style=display:none" for those using special characters may not be seamless. Specifically, current testing of Chrome/Safari shows they mess with CHR(0)-CHR(8) and CHR(10)-CHR(31) assignments to "text" inputs, but only CHR(0) and CHR(10) assignments to "hidden" inputs.

Best practice: depend on nothing, use the smallest vocabulary possible, and pray to your God.

Return to Web Development / Standards Evangelism


Who is online

Users browsing this forum: No registered users and 1 guest