Remembering Info
Comments: 11
The “Remember info” button on comment forms seems to work now. Apologies for it not doing so previously. Took me quite a while to figure this one out (not atypically) because, well, the whole thing seems pretty counterintuitive to me.
Why didn’t it work? Because there was no name attribute in the form (strict XHTML doesn’t allow it, although why for the love of Tim Berners-Lee and all that’s hyperlinked I have no idea). Anyway, I was assuming that the JavaScript would get the form name from the id attribute, which it does not. Adding the name attribute made things work as they should, but the pages weren’t going to validate as strict, so I had to find a way around that.
You can reference elements without using the name attribute, but as David Flanagan writes in JavaScript: The Definitive Guide:
With reasonably chosen names, this syntax [e.g.,
document.myForm.inputName] is much more elegant than the alternative, which relies on hardcoded (and position-dependent) array indices:
document.forms[1].elements[4]
Now even I could have told you that and I know only enough JavaScript to be dangerous. Yet this is the method to which I’ve had to resort. So I tweaked the script that displays the remembered values (the cookies were getting set correctly all along; it’s just that the values weren’t being displayed when you saw the form) and uploaded that and everything seemed to be fine.
Great, I thought, now I’ll have the “Remember info” function working and the pages will validate as strict XHTML.
Not quite. While BBEdit’s validator can find nothing wrong with my pages, the W3C’s validator is telling me there’s an error (which it cryptically labels only as “X”) in the archive pages. This is a line of code that looks fine to me, and is in fact unchanged from previous versions of the template. Go figure. Damned if I know what’s going on at this point.
So one things leads to another. A problem is solved, a new one arises. I’m giving up for the day. It’s late and I may well be missing something startlingly obvious that will leap out at me in the cold hard light of morning. I can only hope so. In the meantime, you should find that your information is being remembered across comment posts which is what I wanted to have happen in the first place. I’m a little further along, but not there yet.
•••
Posted to MetaStuff • 2002.10.29 (Tue) • 00:11
Comments
Posted by pup 2002.10.29, 00:29
<input type=”checkbox” name=”bakecookie” />
you need an id=”bakecookie” in there. i don’t know if that’s the source of the validation error, but your ” ok” label is looking an element with the id of “bakecookie”, and you’ve got no id on the checkbox.
Posted by pup 2002.10.29, 00:32
the w3c’s beta validator is a little more helpful.
Posted by jh 2002.10.29, 00:43
Pup — You’re brilliant. Thank you. I didn’t even see that.
Posted by victor 2002.10.29, 06:00
You can also use:
document.getElementById(‘email’).value=getCookie(“mtcmtmail”);
It should work on any DOM level 1-compliant browser, and validate too.
Posted by Scott 2002.10.29, 10:00
Thanks for the post Jeremy, I was having the exact same promlems as you the other day and simply gave up.
Posted by jh 2002.10.29, 12:05
Victor — I saw the getElementById() as a possible solution, but the JavaScript book didn’t give an example of the syntax that I could find and I don’t know enough that I was able to complete the thought.
Thanks for posting that.
Scott — For someone more comfortable with JavaScript than I am, it may have been obvious. But it all seems so illogical to me.
First, there’s the removal of the “name” attribute from the strict DTD. No doubt there are reasons for this but it seems like such an obvious and helpful thing to have. This leads to the DOM-fiddling which I had to puzzle through to get the values to show up in the form. Now I’m wondering if we’re stuck with this “inelegant” way of getting things done.
Victor’s suggestion seems like a way around that, but for the less technically proficient it seems much more complicated than simply being able to point to named elements. I’m really curious why the “name” attribute was dropped from forms.
Posted by Mike Youell 2005.03.16, 15:18
In regard to the form “name” attribute not being valid in xhtml strict, I found this on the net:
“To get around this issue, I used LESSTHANform id=’blah’ … GREATERTHAN and in my link I used javascript:document.forms[‘blah’].submit(); which validates correctly.”
I have tested this and it seems to work on Mozilla Firefox. Also it validates with w3.org and WDG XHTML validators.
See URL for the source of the solution.
(Note your make safe HTML thing didn’t work for my post).
Posted by ebay 2005.06.21, 03:43
Oh…
Posted by Hoodia 2005.12.13, 13:38
I’m having problems using it on firefox. It could just be a bug on my computer.
Posted by Bob 2005.12.26, 09:30
Very interesting information.
Posted by Cigarettes 2007.04.10, 03:47
C?To get around this issue, I used LESSTHANform id=?blah? ? GREATERTHAN and in my link I used javascript:document.forms[?blah?].submit(); which validates correctly.?
Post a comment:
Send This Story to an Enemy
• • •