Problems with defaultValue

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.

4 Responses to “Problems with defaultValue”

  1. omar ugalde Says:

    I use a and everything went ok.
    Pretty weird dont you think?

  2. splintor Says:

    Hi omar and thanks for responding. I didn’t quite understand what you wanted to say. Can you elaborate please?

  3. omar ugalde Says:

    I had another kind of “problem” with the defaultValue in firefox.
    When you change programmatically the property “value” of an input field of type hidden.
    you change implicitly the defaultValue too.
    That does not happen on IE
    The solution I gave to this difference was to wrap a input of type text in a tag with a
    style=”display:none” or maybe put the style directly into the input field.
    (My first reply did not get complete, I hope this one does).

  4. splintor Says:

    Interesting.
    This might be related to this Firefox bug (I’m to tired now to get into the details of that bug)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: