> For the complete documentation index, see [llms.txt](https://jscula.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jscula.gitbook.io/docs/events/event-listener/onenterkey.md).

# onEnterKey

Triggers a callback function when the Enter key is pressed inside an input field.

### onEnterKey(callback, options)

#### Parameters:

* `callback`: A function to execute when Enter is pressed.
* `options`: (Optional) Additional event listener options.

#### Usage:

```javascript
$('#inputField').onEnterKey(function(e){
    console.log('Enter key is pressed!');
});
```

### Use cases

Use `onEnterKey()` to:

* Start a search from an input field.
* Send a chat message.
* Confirm a short value without clicking a button.

### More examples

Search for the current query:

```javascript
$('#query').onEnterKey(function() {
  search(this.val());
});
```

Send a message when the user presses Enter:

```javascript
$('#message').onEnterKey(function() {
  sendMessage(this.val());
  this.val('');
});
```

{% hint style="info" %}
Use a regular function when you need `this` to refer to the input element.
{% endhint %}
