1

I've this code below:

<div class="search-container">
    <input id="search-value" type="text" autocomplete="off" ng-model="vm.searchQuery" ng-focus="vm.searchBoxHasFocus = true" ng-blur="vm.searchBoxHasFocus = false" class="search-box-input ng-pristine ng-untouched ng-valid ng-empty" placeholder="Pesquisar" ng-enter="vm.goSearch()" ng-change="vm.getSuggestions()">
    <div class="label-search" ng-click="vm.goSearch()"></div>
    <button type="button" class="reset-search" ng-click="vm.clearQuery()"></button>
</div>

How can I call a JS function when I press "enter" in the input?

KmDroid
  • 131
  • 4

4 Answers4

0

If you're looking to do it in raw javascript you can use the below binding to bind the enter key for the whole document to the function you wish to call.

// Load in the enter keybindings
document.onkeydown = function (e) {
    switch (e.which || e.keyCode) {
        case 13 : //Your Code Here (13 is ascii code for 'ENTER')
            vm.clearQuery()
    }   
}   
asdf
  • 2,927
  • 2
  • 21
  • 42
0

you can always wrap it in a form and call onsubmit and just change vm.goSearch() to your actual function.

<form onsubmit="return function(){vm.goSearch();return false;};">
<div class="search-container">
    <input id="search-value" type="text" autocomplete="off" ng-model="vm.searchQuery" ng-focus="vm.searchBoxHasFocus = true" ng-blur="vm.searchBoxHasFocus = false" class="search-box-input ng-pristine ng-untouched ng-valid ng-empty" placeholder="Pesquisar" ng-enter="vm.goSearch()" ng-change="vm.getSuggestions()">
    <div class="label-search" ng-click="vm.goSearch()"></div>
    <button type="button" class="reset-search" ng-click="vm.clearQuery()"></button>
</div>
</form>
imvain2
  • 15,480
  • 1
  • 16
  • 21
0

You can try with something like this:

// Get the input field you want
    var input = document.getElementById("search-value");

    // Execute a function when the user releases a key on the keyboard
    input.addEventListener("keyup", function(event) {
      // Cancel the default action, if needed
      event.preventDefault();
      // Number 13 is the "Enter" key on the keyboard
      if (event.keyCode === 13) {
        // Trigger the button element with a click
        document.getElementById("ElementId").click();
        //or function call here
      }
    }); 

Here is a useful tips

handle events with HTML/DOM

leonite
  • 1
  • 1
0

try ng-keypress event and read keyCode = 13 for enter.

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js" > 
</script>
<body>

 <div ng-app="myApp" ng-controller="myCtrl">

 First Name: <input type="text" ng-model="firstName"><br>
 Last Name: <input type="text" ng-model="lastName" ng-keypress="keypress($event)"> 
 <br>
 <br>


 </div>

 <script>
 var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {

$scope.keypress=function(e) {
if(e.keyCode == 13) {
 console.log('Enter');
 }
}  
});
</script>

Sunil
  • 138
  • 2
  • 15