-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Terry Riegel wrote:
>>> I would prefer to not have the id in the form.
> I am also in the habit of not naming my forms.

Heh, you seem to have lotta PITA habits ;-)

> It seems to me this would be common for someone to want to $() a form
> element, but alas I hear no other solutions.

Remember there's one more "dollar function" you could use: "$$()" [1]
Use that (or build yourself a variation) for alleviating your "habits", all
you need is studying some CSS/XPath and - duh - knowing your DOM.
For instance:
  $$('form[action="/contact"] input[name="email"]')[0]
would return you the input field named "email" from the form that has the
action set to "/contact".
If you already have the (extended) form object, you could use:
  f.down('input[name="email"]')
for the same purpose.

To simplify it, here's a possible variation:

// add your method to "Form.Methods"
Object.extend(Form.Methods, {
  getElement: function(form, name) {
    form.down( '*[name="' + name + '"]' );
  }
});
// apply new method to "Form" elements
Object.extend(Form, Form.Methods);

and now you'll be able to use:
 var emailField = f.getElement('email');

IAE, "getElement" is a bad name, as Prototype folks may choose to use it for
something else in the future, choose your own "tricky" name ;-)

OTOH, OFC, you could always use Ken's advice going down the "elements" path
as it should be more efficient for nowadays browsers.


> I am building my parameters to pass to my ajax request like...
> var pars = '?ajaxrequest='+$F('mycomment');

YA bad (YMMV) habit :))
Prototype can help you big time with this kind of things. Please (re)read
Prototype's introductory article to "ajax" [2], especially the "Parameters
and the HTTP method" section. Briefly, Prototype will handle all your
parameters encoding.

>> And it "mostly" works. How would I urlencode $F('mycomment')?
> ahhh just found it its... escape()
Do yourself a favor, NEVER - I mean it - NEVER use (un)escape!
These methods are deprecated [3] and very error prone *because of their
misuse* (just search this list's archive and you'll see what I meant).
Refer to encodeURIComponent & co for such functionality: [4], [5].

[1] http://prototypejs.org/api/utility/dollar-dollar
[2] http://prototypejs.org/learn/introduction-to-ajax
[3]
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Deprecated_Features#Escape_Sequences
[4]
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Functions:encodeURIComponent
[5] http://msdn2.microsoft.com/en-us/library/aeh9cef7.aspx

'HTH
- --
Marius Feraru
-----BEGIN PGP SIGNATURE-----

iD8DBQFGaJgAtZHp/AYZiNkRAr6GAKDlj9RymxNmJZ34SbEbHGizrTdbOQCfdrvZ
uIrJksmp5xsflO50Ful1jUA=
=BvRf
-----END PGP SIGNATURE-----

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to