Archive for the ‘Forms’ Category

Problems with defaultValue

June 14, 2006

Today I found a problem in one of our screens (the filter screen) which only occurs on Firefox, but not on IE. After checking it further, I realized that the problem is with the defaultValue attribute of the <input type=text> element.

We use this attribute to store the default value of filter fields, and when the user wants to reset the filter, we set the value to the default value manually, without calling reset(). The default filter value can be set by the server administrator.

It appears that on Firefox, setting the defaultValue attribute causes the control to change its value if the user didn't change it yet. The logic is probably that if the user didn't change it, the control shows the default value, so changing the defaultValue attribute changes what the control displays, and the control's value attribute.

On IE, on the other hand, the MSDN clearly says that "The value of the property can be changed programmatically, but doing so has no effect on the appearance of the object or the submitted value."

I wanted to see what the HTML standard has to say about it, but apparently, the defaultValue attribute is a non-standard extension, supported by both IE and FF.

The solution to our problem is very simple. Use another custom attribute (e.g. defValue) to store the default value. This will have no affect, has the browsers have no special treatment for this custom attribute.