Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Count Characters in Input Fields (See related posts)

Stick these methods in helpers/application_helper.rb:
  def countdown_field(field_id,update_id,max,options = {})
    function = "$('#{update_id}').innerHTML = (#{max} - $F('#{field_id}').length);"
    count_field_tag(field_id,function,options)
  end
  
  def count_field(field_id,update_id,options = {})
    function = "$('#{update_id}').innerHTML = $F('#{field_id}').length;"
    count_field_tag(field_id,function,options)
  end
  
  def count_field_tag(field_id,function,options = {})  
    out = javascript_tag function
    out += observe_field(field_id, options.merge(:function => function))
    return out
  end


And then in your view, you can use them like:
<input id="message">
<p>You have used <span id="counter">...</span> letters.</p>

<%= count_field('message','counter') %>

You need to create an account or log in to post comments to this site.


Click here to browse all 4852 code snippets

Related Posts