prototype form serializer gotcha

In learning JavaScript / AJAX, I’ve been using the Prototype library, and I’ve been really happy with it.  It’s quickly gotten me able to wrap my head around what the possibilities are, as well as providing some good docs with great examples.

I hit a snag with the Form.serialize function today that took me a while to figure out what was going on.  Blame it on a confusing doc, because this certainly could have been explained with more clarity.

The Prototype doc (the old one, the new one is hard to navigate and has less information) says that if you pass true to the getHash parameter, it will return “an object hash.”  A careful reading of that should be implied, because it returns a JavaScript hash, not a Prototype Hash object.  Big difference.

I hit upon the problem because I would serialize my form, then try to add more keys to it, using the set function.  That would throw errors, and I couldn’t figure out why.

So, there’s a couple of ways around it.  The simplest one I like is to immediately create it as a Prototype Hash, so you can do what you would normally do.

var h = $H($(‘form’).serialize(true));

Leave a Reply