Submit Message By Pressing Enter?
I am working on a chat app built with Meteor based off of this tutorial (http://code.tutsplus.com/tutorials/real-time-messaging-for-meteor-with-meteor-streams--net-33409) and I am
Solution 1:
You need to check for a key press event. You can find a full list of the codes for each key here: http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
Best way to do this would be make the function in the click event a named function and then you can just run the same function on both events.
Template.chatBox.events({
"click #send": function() {
var message = $('#chat-message').val();
chatCollection.insert({
userId: 'me',
message: message
});
$('#chat-message').val('');
//add the message to the stream
chatStream.emit('chat', message);
},
"keypress #chat-message": function(e) {
if (e.which == 13) {
console.log("you pressed enter");
//repeat function from #send click event here
}
}
});
Solution 2:
'keypress #send': function (evt) {
if (evt.which === 13) {
13 is the keycode for enter. If you want to change it you can look up the javascript keycodes so you can bind to whatever you like.
You might want to consider familiarizing yourself with the apihttp://docs.meteor.com/
Post a Comment for "Submit Message By Pressing Enter?"