Skip to content

Instantly share code, notes, and snippets.

@neerajsingh0101
Created January 11, 2009 04:04
Show Gist options
  • Save neerajsingh0101/45630 to your computer and use it in GitHub Desktop.
Save neerajsingh0101/45630 to your computer and use it in GitHub Desktop.
<%= text_area 'comment', 'comment', "rows" => 10,:cols => 55, :class => 'autoexpand_textarea' %>
// http://jroller.com/rmcmahon/entry/resizingtextarea_with_prototype
// Usage: <% apply_behaviour "textarea", "new ResizingTextArea(this);" %>
var ResizingTextArea = Class.create();
ResizingTextArea.prototype = {
defaultRows: 1,
initialize: function(field)
{
this.defaultRows = Math.max(field.rows, 1);
this.resizeNeeded = this.resizeNeeded.bindAsEventListener(this);
Event.observe(field, "click", this.resizeNeeded);
Event.observe(field, "keyup", this.resizeNeeded);
},
resizeNeeded: function(event)
{
var t = Event.element(event);
var lines = t.value.split('\n');
var newRows = lines.length + 1;
var oldRows = t.rows;
for (var i = 0; i < lines.length; i++)
{
var line = lines[i];
if (line.length >= t.cols) newRows += Math.floor(line.length / t.cols);
}
if (newRows > t.rows) t.rows = newRows;
if (newRows < t.rows) t.rows = Math.max(this.defaultRows, newRows);
}
}
Event.addBehavior({
'.autoexpand_textarea:focus':function(e) {
new ResizingTextArea(this);
}
})
<%= javascript_include_tag "prototype", "effects","lowpro","application", :cache => "cache/all" %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment