Alan2
Alan2

Reputation: 24582

How can I wire up a button on my page so that it is clicked when a user presses enter?

I have many buttons on my page but none are inside a form. I already assigned one button and changed its color so it shows as a bootstrap primary button.

How can I make it so that when a user is on the page and they click enter then the button click event for that button is called?

Upvotes: 0

Views: 358

Answers (3)

Pankaj Parkar
Pankaj Parkar

Reputation: 136174

ng-submit will not useful to you, because your form submit event is different.

The only option remains is, you can take use of ng-enter directive, but you should manually bind it wherever you want. I believe this is last option.

ng-enter="myEvent()"

HTML

<input type="text/url/email/number/checkbox/radio" ng-model="test" ng-enter="myEvent()"/>

Hope this could help you, Thanks.

Upvotes: 0

user3681587
user3681587

Reputation: 566

I am not sure about what you are asking; but you can do the following, if you have a controller with the function add() attached to the scope:

<form ng-submit="add()">
  <input type="text" ng-model="myModel">
  <input type="submit" value="Add"/>
</form>

The above adds the value entered in the input to the model.

Upvotes: 1

ngDeveloper
ngDeveloper

Reputation: 1304

I suggest using a <form ...> around your elements. Because if you rely on a custom keypress detector, and you have more than one button, it will activate all of them. The <form ...> dictates which button is to be submitted when a user is in a particular input field and hits Enter.

Your form doesn't even need to have an action="..." or method="...". You could just as well use ng-submit="yourFormParser()".

Upvotes: 0

Related Questions