Hello All, This is my first post. I have a form element like... <input type="text" name="mycomment" id="mycomment"> I would prefer to not have the id in the form. If I remove it the dollar function doesn''t work anymore (for obvious reasons) is there another way to grab the object? I would rather code it as... <input type="text" name="mycomment"> Thanks for any help/direction. Terry --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Terry Riegel wrote:> ...I would prefer to not have the id in the form. If I remove it the > dollar function doesn''t work anymore (for obvious reasons) is there > another way to grab the object? > > I would rather code it as... > <input type="text" name="mycomment"> > ... >Hi Terry, I also avoid putting ids onto inputs since they can be accessed through the forms and elements collections: document.forms[''FormName''].elements[''InputName'']; or document.FormName.elements[''InputName'']; In fact, I use a custom function (below) as shorthand: SF(''FormName'',''InputName''); -- Ken Snyder // // SmartForm Function // returns form object or element object from form name, element name // // Example Usage: // Field(0,''myelement'') returns form 0 element "myelement" // Field(''myform'',''myelement'') returns form "myform" element "myelement" // Field(0) returns form form 0 // Field(''myform'') returns form "myform" assuming there is no element named "myform" // Field(''myelement'') returns form "myelement" assuming there is no form named "myelement" function SF(formName,elName) { if( elName && document.forms[formName] && document.forms[formName].elements ) { return document.forms[formName].elements[elName]; } else { return document.forms[formName]; } } --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Ken, I am also in the habit of not naming my forms. So this solution would involve naming the forms. Something I have done in the past. It seems to me this would be common for someone to want to $() a form element, but alas I hear no other solutions. I have another question while were on the topic of forms. I am building my parameters to pass to my ajax request like... var pars = ''?ajaxrequest=''+$F(''mycomment''); And it "mostly" works. How would I urlencode $F(''mycomment'')? Thanks for any help Terry On Jun 7, 2007, at 1:28 PM, Ken Snyder wrote:> > Terry Riegel wrote: >> ...I would prefer to not have the id in the form. If I remove it the >> dollar function doesn''t work anymore (for obvious reasons) is there >> another way to grab the object? >> >> I would rather code it as... >> <input type="text" name="mycomment"> >> ... >> > Hi Terry, > > I also avoid putting ids onto inputs since they can be accessed > through > the forms and elements collections: > > document.forms[''FormName''].elements[''InputName'']; > or > document.FormName.elements[''InputName'']; > > In fact, I use a custom function (below) as shorthand: > SF(''FormName'',''InputName''); > > -- Ken Snyder > > > > // > // SmartForm Function > // returns form object or element object from form name, element name > // > // Example Usage: > // Field(0,''myelement'') returns form 0 element "myelement" > // Field(''myform'',''myelement'') returns form "myform" element > "myelement" > // Field(0) returns form form 0 > // Field(''myform'') returns form "myform" assuming there is no element > named "myform" > // Field(''myelement'') returns form "myelement" assuming there is no > form > named "myelement" > function SF(formName,elName) { > if( elName && document.forms[formName] && > document.forms[formName].elements ) { > return document.forms[formName].elements[elName]; > } else { > return document.forms[formName]; > } > } > > > >--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
ahhh just found it its... escape() Thanks, Terry On Jun 7, 2007, at 4:56 PM, Terry Riegel wrote:> > Thanks Ken, > > I am also in the habit of not naming my forms. So this solution would > involve naming the forms. Something I have done in the past. It seems > to me this would be common for someone to want to $() a form element, > but alas I hear no other solutions. > > I have another question while were on the topic of forms. > > I am building my parameters to pass to my ajax request like... > > var pars = ''?ajaxrequest=''+$F(''mycomment''); > > And it "mostly" works. How would I urlencode $F(''mycomment'')? > > Thanks for any help > > Terry > > > > > On Jun 7, 2007, at 1:28 PM, Ken Snyder wrote: > >> >> Terry Riegel wrote: >>> ...I would prefer to not have the id in the form. If I remove it the >>> dollar function doesn''t work anymore (for obvious reasons) is there >>> another way to grab the object? >>> >>> I would rather code it as... >>> <input type="text" name="mycomment"> >>> ... >>> >> Hi Terry, >> >> I also avoid putting ids onto inputs since they can be accessed >> through >> the forms and elements collections: >> >> document.forms[''FormName''].elements[''InputName'']; >> or >> document.FormName.elements[''InputName'']; >> >> In fact, I use a custom function (below) as shorthand: >> SF(''FormName'',''InputName''); >> >> -- Ken Snyder >> >> >> >> // >> // SmartForm Function >> // returns form object or element object from form name, element name >> // >> // Example Usage: >> // Field(0,''myelement'') returns form 0 element "myelement" >> // Field(''myform'',''myelement'') returns form "myform" element >> "myelement" >> // Field(0) returns form form 0 >> // Field(''myform'') returns form "myform" assuming there is no element >> named "myform" >> // Field(''myelement'') returns form "myelement" assuming there is no >> form >> named "myelement" >> function SF(formName,elName) { >> if( elName && document.forms[formName] && >> document.forms[formName].elements ) { >> return document.forms[formName].elements[elName]; >> } else { >> return document.forms[formName]; >> } >> } >> >>> >> > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
-----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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---