I've looked all over Google and StackOverflow and looked through the best_in_place javascript code to no avail.
I am using best_in_place to edit a property of a Post
model by using a textarea, like so:
<%= best_in_place @post, :summary,
{ :type => :textarea, :display_with => 'simple_format'} %>
I want to be able to trigger best_in_place's submit function(s) when hitting Enter/Return unless it's pressed with the shift key (which would allow a carriage return), just as it would when clicking outside of the editable area. With a regular text/input, this is default functionality (minus the carriage return part). However when using textareas, it is not.
I've got a nice piece of jQuery pulled from here that handles identifying when Enter with/without shift is pressed, but I can't figure out how to trigger best_in_place to submit the data.
//Have to bind it to the activate function, or else
//the element won't be in the DOM when it tries to identify it
$('.best_in_place').bind('best_in_place:activate', function(){
$(this).find('textarea').keyup(function(e) {
var $textarea = $(this);
if(e.keyCode == 13 && !e.shiftKey) {
e.preventDefault(); // Don't make a new line
// This line does weird things
$(this).parents('.best_in_place').trigger('best_in_place:update').trigger('best_in_place:deactivate');
}
});
});
I also have some functions bound to the best_in_place:update
function, which basically add & remove classes for styling. With the above code, when I hit enter within a best_in_place textarea, the functions that I have bound to the best_in_place:update
function fire, but no data is submitted to the server, as I can see in the console.
TL;DR; How can I trigger best_in_place to submit/update and deactivate on hitting the Enter key, just as it would when clicking outside the editable area?