14

I'm using Rails 3 with jQuery, and have a simple form with a single text input and a submit button. I have the form submitting with :remote => true, and would like to clear the input after the form is submitted. I tried this jQuery code:

$('#new_message').submit(function() {
  $('#message_content').val('');
});

However, this clears the input before the form is submitted, so I end up submitting a blank form.

After searching around a bit, I also tried the following version:

$('#new_message').submit(function(e) {
  e.preventDefault();
  this.submit();
  $('#message_content').val('');
});

...but this seems to override the :remote => true magic, and the form gets submitted with a full page reload. Any ideas?

5 Answers 5

24

When submit button is pressed for a form marked remote => true, javascript code in rails.js does the submit(). You need not override the functionality.

Try this instead in $(document).ready -

(Edit: Using bind instead of live. Thanks Darren Hicks.)

$("#new_message").bind("ajax:complete", function(event,xhr,status){
  $('#message_content').val('');
}

This adds an event handler for the ajax:complete event. This event is fired when your the ajax-submit-form is completed. More here.

Edit:

Don't forget closing ) on the bind method.

$("#new_message").bind("ajax:complete", function(event,xhr,status){
   $('#message_content').val('');
 })
Sign up to request clarification or add additional context in comments.

5 Comments

Nice! I used your code but changed the event to ajax:beforeSend, which clears the input immediately upon submission (instead of waiting for the request to actually go through), but still sends the data correctly. My thanks for digging this up!
How can you do this in Coffeescript?
.live() is deprecated for jQuery. You want to use .bind() instead in the answer above! stackoverflow.com/questions/14526033/…
@tibbon coffeescript: $("body").on 'ajax:complete', '#new_message', (event,xhr,status) -> $('#message_content').val('')
You could always respond_to :js and clear the form in your create.js.erb file: $('form#new_message').find("input[type=text], textarea").val("")
1

In your action.js.erb file

$('#message_content').attr('value','');

Where action is the method name (which you don't state in your question) in your controller

3 Comments

I thought of this, but it would mean waiting for the entire request/response to go through before the input is cleared, which feels rather laggy (and might result in multiple form submissions if the user gets impatient). abhishek's event binding solution, using ajax:beforeSend, clears the input immediately.
fair enough, your user might be annoyed if the ajax call fails for any reason and you've cleared their input box. Do they get any other visual clue that the request has succeeded?
The interface in question is a basic chatroom, so when the request returns, the user's message appears in the chat. I think in this scenario responsiveness is more important than being able to resubmit your message without retyping it, though I guess I could also save the submitted message client-side and display it back to the user (allowing them to copy-paste it) if the ajax fails.
0

In your jquery function, do the submitting yourself via get(). You set the text field to blank right after calling get(). Or, you can do this in the success callback that you provide to get(), which will let you get some info back from your controller, like whether the submitted text field value was acceptable.

1 Comment

Hmm... I don't doubt that this works, but it would seem to defeat the point of using :remote => true in the first place, since you're pretty much reimplementing all of its functionality. I might do this as a last resort, but I'd be surprised (and a bit disappointed) if this is the only way to do it.
0

You can trigger a Rails AJAX submit like this:

$(this).trigger('submit.rails');

Comments

0

In your model.js file

               $('#element_id').val(' ');

It work's for me, you can try this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.