views:

103

answers:

2

I have something like this in my web forms:

<input type="hidden" name="myField" value="defaultValue" />

Later on, in some Javascript, I am overwriting the default value before I submit the page to the server.

var formField = document.getElementsByName("myField")[0];
formField.setAttribute("value", "myNewValue");
var form = document.getElementById("myForm"); 
form.submit();

All browsers (I tested this code over years in IE5-IE8, Firefox, Opera, Chrome, Safari, ...) are sending "myNewValue" to the server. Except IE9: It sends "defaultValue". What's going on here? Am I missing something?

If I remove the "value" attribute from the field, it also works in IE9. It also works in IE9 if I switch to IE8 rendering mode.

Is this a bug or is IE9 more standards compliant than the other browsers?

+2  A: 

The value attribute is defined as setting the initial value.

Once the form has been loaded, the field will have it's actual value set to it's initial value.

As far as I know, there is nothing that says the current value should change if the initial value changes after the form has loaded, so I would suspect that this is a bug fix and not a new bug.

Use the value property instead of the setAttribute method.

David Dorward
Are you telling me that formField.value is setting the current value while formfield.setAttribute("value") is trying to overwrite the initial value? So, IE9 has got it right now and all other browsers are doing this wrong?
newtogit
@newtogit: That's actually how a lot of things in IE9 happen to be now. Yeah, it's a bit freaky if you consider IE's history.
Esko
It looks that way. There might be something in the spec to say it should be different but I haven't seen it (nor gone out of my way to look for it though).
David Dorward
.value should be the same as setAttribute("value"). I just tested it: and it is. Also when I use .value the value is not sent to the server if I had an initial value. Still looks like a bug to me. I can't use value="initial value" in forms if I intend to set a current value later on as this current value is not submitted. I am using XHTML 1.0 strict by the way.
newtogit
A: 

David Dorward appears to be correct as regards to HTML4, but this seems to be backward-compatibility breaking behaviour, so I would expect HTML5 to describe what browsers actually do. And it seems to. It says this: (my emphasis)

The value content attribute gives the default value of the input element. When the value content attribute is added, set, or removed, if the control's dirty value flag is false, the user agent must set the value of the element to the value of the value content attribute, if there is one, or the empty string otherwise, and then run the current value sanitization algorithm, if one is defined.

See http://dev.w3.org/html5/spec/the-input-element.html#attr-input-value

Your scenario doesn't describe anything that would set the dirty value flag prior to the setAttribute call, so I believe that the setAttribute call should set the value property and the browser should send myNewValue not defaultValue.

So yes, I think it is a IE9 Beta bug.

Alohci
So, what would be the HTML 4 correct way to set the content value? .value doesn't do the trick. What would I need to do to make IE9 sent myNewValue? Any ideas? Thanks for digging this up!
newtogit