views:

19

answers:

1

Here is the chat submit form:

  #chat_form
    - remote_form_for :chat, :url=>{:controller => "chats", :action=> "new", :id=>@request.id }, :html => { :autocomplete => 'off' } do |f|
      = f.text_area "message_content", :rows=>5, :cols=>55, :autocomplete => "off"
      = f.submit(value = "Sayit!", :class => "submit small")

chat#new.js.rjs

page << "$('#chat_message_content').val('')"
page.visual_effect :highlight, "chat_form"

Chats controller:

  def new
    @request = Request.find(params[:id])
    @chat = Chat.new(params[:chat])
    @chat.created_at = Time.now
    @chat.user_id = current_user.id
    @request.chats <<@chat
    render :layout => false
  end

"Sayit!" button is nice, but i need message sending by "Enter" button.

How it can be done ?

(i'm using jQuery)

+1  A: 

You can attach a handler to the text area element that will catch enter, maybe like so:

$('#message_content').keypress( function( e ) {
  if( e.keyCode == 13 ) { $(this).closest('form').trigger('submit'); }
} );

But then people can't put newlines into the text area which is pretty counter intuitive. For single-line inputs you probably want a regular text field (input element) which will trigger submit of the form it's in when the user presses enter.

thenduks