Hi! I''m looking for the best way to make a countdown character while typing in a textfield input with prototype, Anybody could help me? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
1) Put a div somewhere close to your textarea. Give it an Id. 2) Add a maxlength attribute to your textarea field 3) Call this from your onload function: Event.Observe(''<id of your textarea>'', ''keypress'', function(e) { var el = Event.element(e); var charsLeft = parseInt(el.getAttribute(''maxlength'')) - el.length; if (charsLeft > 0) { $(''<id of your div'').update(''''+ charsLeft + '' character(s) left.''; } else { Event.stop(e); } }); I haven''t tested the javascript code, I just wrote it direct into the message window, so you might need to tweak it. Gareth On 7/20/07, symfo <Ismael.Liceras-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Hi! > > I''m looking for the best way to make a countdown character while > typing in a textfield input with prototype, Anybody could help me? > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> .. the best way to make a countdown character while typing in atextfield .. The simplest way : <form> chars left : <span id="counter"><%= maxcounter=10 %></span> <%= text_field_tag :description, nil, :maxlength => maxcounter, :onkeyup => "$(''counter'').update(#{maxcounter}-this.value.strip().length)" -%> </form> -- Alain Ravet -------- http://blog.ravet.com --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Is there also a way without ruby to do this ? On Jul 22, 7:00 pm, "Alain Ravet" <alain.ra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > .. the best way to make a countdown character while typing in a > textfield .. > > The simplest way : > > <form> > chars left : <span id="counter"><%= maxcounter=10 %></span> > <%= text_field_tag :description, nil, > :maxlength => maxcounter, > :onkeyup => > "$(''counter'').update(#{maxcounter}-this.value.strip().length)" > -%> > </form> > > -- > Alain Ravet > --------http://blog.ravet.com--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
That would be what I posted. On 8/12/07, Mech7 <chris.de.kok-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Is there also a way without ruby to do this ? > > On Jul 22, 7:00 pm, "Alain Ravet" <alain.ra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > .. the best way to make a countdown character while typing in a > > textfield .. > > > > The simplest way : > > > > <form> > > chars left : <span id="counter"><%= maxcounter=10 %></span> > > <%= text_field_tag :description, nil, > > :maxlength => maxcounter, > > :onkeyup => > > "$(''counter'').update(#{maxcounter}-this.value.strip().length)" > > -%> > > </form> > > > > -- > > Alain Ravet > > --------http://blog.ravet.com > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Jul 20, 7:58 am, "Gareth Evans" <agr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 1) Put a div somewhere close to your textarea. Give it an Id. > 2) Add a maxlength attribute to your textarea fieldMaxlength is not a valid attribute for text area elements - though some don''t care about that.> 3) Call this from your onload function: > > Event.Observe(''<id of your textarea>'', ''keypress'', function(e) {Event.observe(...> var el = Event.element(e); > var charsLeft = parseInt(el.getAttribute(''maxlength'')) - el.length;The use of parseInt is not required, the subtraction operation will peform any required type conversion. Even Prototype.js extended elements don''t have a length attribute, try: ... el.value.length> if (charsLeft > 0) { > $(''<id of your div'').update(''''+ charsLeft + '' character(s) left.'';Missing closing bracket.> > } else { > Event.stop(e);Do you really want to do that? Once maxlength is reached, *all* input is prevented so users can''t change whatever they''ve typed. You should allow keystrokes like delete, cursor keys, backspace, etc. It is much simpler and more userfriently to let users know when they''ve put too many characters in the text area and let them trim it manually.> } > }); > > I haven''t tested the javascript code, I just wrote it direct into the > message window, so you might need to tweak it.Yes. -- Rob --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Yeah my bad Rob, thanks for correcting the errors. It was intended as a guide tho, not a bullet proof example.. Gareth On 8/13/07, RobG <rgqld-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote:> > > On Jul 20, 7:58 am, "Gareth Evans" <agr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > 1) Put a div somewhere close to your textarea. Give it an Id. > > 2) Add a maxlength attribute to your textarea field > > Maxlength is not a valid attribute for text area elements - though > some don''t care about that. > > > > 3) Call this from your onload function: > > > > Event.Observe(''<id of your textarea>'', ''keypress'', function(e) { > > Event.observe(... > > > > var el = Event.element(e); > > var charsLeft = parseInt(el.getAttribute(''maxlength'')) - el.length; > > The use of parseInt is not required, the subtraction operation will > peform any required type conversion. > > Even Prototype.js extended elements don''t have a length attribute, > try: > > ... el.value.length > > > if (charsLeft > 0) { > > $(''<id of your div'').update(''''+ charsLeft + '' character(s) left.''; > > Missing closing bracket. > > > > > } else { > > Event.stop(e); > > Do you really want to do that? Once maxlength is reached, *all* input > is prevented so users can''t change whatever they''ve typed. You should > allow keystrokes like delete, cursor keys, backspace, etc. > > It is much simpler and more userfriently to let users know when > they''ve put too many characters in the text area and let them trim it > manually. > > > } > > }); > > > > I haven''t tested the javascript code, I just wrote it direct into the > > message window, so you might need to tweak it. > > Yes. > > > -- > Rob > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---