Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

input text return event in Meteor

Tags:

I want to capture the event of a user pressing enter on an input of type="text" when they are filling out a form. This is done all over the web, yet the answer eludes me.

This is what I have so far:

In the html file, I have a text input like so:

<input type="text" size=50 class="newlink">

In the Javascript file, I am trying to capture the the user pressing enter to effectively submit the form. I am then grabbing the text from the input and going to stash it in the database:

  Template.newLink.events = {
    'submit input.newLink': function () {
      var url = template.find(".newLink").value;
      // add to database
    }
  };
like image 386
chet Avatar asked Oct 22 '12 11:10

chet


1 Answers

The submit event is emitted from forms, not single input elements.

The built in event map for meteor is documented here: http://docs.meteor.com/#eventmaps.

You'll have to listen for a keyboard event (keydown, keypress, keyup). Within the event handler, check, if it's the return/enter key (Keycode 13), and proceed on success.

Template.newLink.events = {
  'keypress input.newLink': function (evt, template) {
    if (evt.which === 13) {
      var url = template.find(".newLink").value;
      // add to database
    }
  }
};
like image 83
Andreas Avatar answered Oct 26 '22 05:10

Andreas