views:

45

answers:

3

Bug: http://www.chubbyfish.co.uk/bug.html

I'm dynamically creating the 3rd input field, and then adding a value to it (of "input 3 value").

Refresh the page several times and you'll see input4 and input5 get filled up with the same value. Why?

Any ideas?! If you can fix it I'll buy you a luxury yacht and a new house!

+1  A: 

Try using document.getElementById instead of the older, deprecated (not recommended) document.forms object. So, for example, replace:

document.forms[0].input3.value = 'input 3 value';

with:

document.getElementById('input3').value = 'input 3 value';

Once this is done, give each input tag a name attribute. For convenience, give it the same name as the id. Form elements work with names.

Also, this is unrelated but it's a good idea to have a standards-based doctype to avoid rendering in 'quirks mode' (an old engine mode used for compatibility with old sites). You can do this by sticking this code at the very start of the HTML page:

<!DOCTYPE html>

Addition: since you are using HTML and not XHTML, you shouldn't use the self-close notation for the input tags (you should remove the slash that is right before the >).

Delan Azabani
The is nothing wrong (or deprecated) about `document.forms`. However chubbyfish is using it wrong here.
RoToRa
Thanks. I've updated the page and it now validates as HTML5. Bug still exists!
chubbyfish
This may not do anything but even if it doesn't, it's good practise. Please remove the trailing self-close slash at the end of the `input` and `meta` tags. This self-closing syntax should not be done in HTML.
Delan Azabani
Better and correct would be `document.forms[0].elements["elementName"]` (elementName of course beeing it's `name` not it's `id`.
RoToRa
Then why not just use `document.getElementById` instead of being stuck in the past?
Delan Azabani
That is not "stuck in the past". It's a valid, non-deprecated, well readable, well documented, well supported and most reliable way to reference form elements.
RoToRa
+1  A: 

I would say there seems some kind of bug in Firefox's autofill.

Try giving the inputs names (which the usual way for inputs anyway). Maybe that will clear it up.

RoToRa
Dang. I'm sure this answer wasn't here when I first looked at the page but given that was only a few minutes ago I must have been blind. I agree with you and have a bit more observed data to support the fact to go with. +1 for saying what I said but 25 minutes earlier. :)
Chris
Yes. Well done. You win a luxury yacht. Adding name attributes fixes the issue. Thanks Chris and RoToRa!! :-)
chubbyfish
@chubbyfish: jsut stick em in the post to me would ya? :)
Chris
@@chubbyfish: And don't forget to mark one of the answers as the accepted answer. Probably RoToRa's since he gave the answer first but if you particularly liked my extra detail feel free to mark mine. You do this with the little ticky under the answer.
Chris
+1  A: 

The reason seems to be in firefoxes "remembering" of entered form values on page refresh. Using firebug I put a break point on the first line of JS and when hitting refresh it renders four text boxes with the same text in as the first four boxes that were already on the page [1]. So the third displayed textbox has the "input 3 value" in the textbox. Then when the new box is inserted that third box becomes number four. I am wondering if this is because firefox ties up the values based on the name attribute that I notice you are missing. Its possible that adding this in will fix it.

I'd suggest going and finding Firefox's bug tracker and seeing if this is already reported. If not then report it with a repro page. If it works fine when the inputs have names though it might be considered not to be a bug - I couldn't really say.

[1] An interesting thing to note is that the "this is overwritten on refresh" text doesn't seem to be replicated down if I just load the page and hit refresh. However, if I modify that field it is clearly seen to be moving down the page as well. Why this is I'm not sure.

Chris
+1 for a for a more complete version of my answer.
RoToRa
Thanks. As I said above, name attribute fixes it. Nice one. Thanks very much!
chubbyfish
@RoToRa: Aww... Mutual love and respect. :)
Chris