Thanks to Colin Law, Timothy G., Stephan Wehner, Greg Akins, Gautam Pai. 
Here is what I got.

1. Ajax is a bad idea for this. It will waste bandwidth and more 
importantly makes the character counting unreliable. You do not know 
when the response will be.

2. Javascript is supposed to be the tool to handle this type of tasks. 
Both jquery and prototype does the work. I did not know which event to 
respond to and how to attach event handlers to the element. Now I know.

a) Default place for the javascript code. "application.js". No other 
changes is needed. However, if you would like to load code like this 
dynamically into your application.js, you are welcome to do so.

b). For jquery, the code could be the following:
====================================================================
$(document).ready(function () {
  $('#id_of_text_area').keyup(function(){
    var s = 100 - $(this).val().length;
    $('#id_of_message').text("You have " + s + " characters left");
      });
});
====================================================================

The key thing which frustrated me was the $(document).ready. That makes 
sure the code is executed at the time when the page is ready, thus 
establishing keyup event handler for .keyup event of the element of 
concern, i.e. the one whose input needs to be counted. The same is true 
for any event handler if it needs to be established by the time the page 
is ready.

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to