# Event Listeners

Event listeners allow you to automatically call any Whisk SDK [basic method](https://docs.whisk.com/shopping-list-sdk/basic-setup/methods) in response to user actions.

* [Click Listeners](#click-listeners)
* [Page Load Listener](#page-load-listener)

## Click Listeners

Click listeners allow you to call any Whisk SDK [basic method](https://docs.whisk.com/shopping-list-sdk/basic-setup/methods) in response to user click on predefined DOM element. In order to create a click listener call

```javascript
whisk.listeners.addClickListener(element_id, method_name, method_parameters)
```

function, which will add an event listener to DOM element with element\_id id attribute. This listener will call the method name method with method\_parameters.

method\_parameters supports the same parameters as a method\_name does, plus one extra non-required parameter:

| ATTRIBUTE | TYPE    | DESCRIPTION                                                                                                                                                                                                                                                                                    |
| --------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| trackView | boolean | If you're planning to use multiple buttons with a click listener on a single page, you might consider passing false to all buttons except one to eliminate duplicate view events (impressions) sent to Whisk analytics. Note: This field won't affect subscription events. Default value: true |

Here is a simple example of usage:

```javascript
whisk.queue.push(function() {
    whisk.listeners.addClickListener('button1', 'shoppingList.addRecipeToList', {
    recipeUrl: 'https://whisk.com/demo/calzone-roasted-peppers',
    });
});
```

**Add recipe to shopping list**

Note: recipeUrl is optional and can be excluded. If it is not included, the URL of your page (which this code is used on) will be taken as the recipe URL.

In order to remove click listener call&#x20;

`whisk.listeners.removeClickListener(element_id)` with the same `element_id` you used in whisk.listeners.addClickListener.

If you need to update parameters for existing click listener, you can simply call whisk.listeners.addClickListener on the same element\_id with new values.

### Subscriptions

Read general information about subscriptions [here](https://docs.whisk.com/shopping-list-sdk/basic-setup/subscriptions). Click listeners support following event types:

* view - the element to which click listener is attached appeared in the viewport. Triggered once. No custom data for this event type.
* click - the element to which click listener is attached has been clicked. No custom data for this event type.

## Page Load Listener

Page load listeners allow you to call any Whisk SDK [basic method](https://docs.whisk.com/shopping-list-sdk/basic-setup/methods) in response to page load when`whisk-show=1` parameter is in the page URL. In order to create a page listener call

```javascript
whisk.listeners.addShowListener(method_name, method_parameters)
```

function. This listener will call `method_name`method with `method_parameters`. Here is simple example of usage:

```javascript
whisk.queue.push(function() {
     whisk.listeners.addShowListener('shoppingList.addRecipeToList', {
     recipeUrl: 'https://whisk.com/demo/calzone-roasted-peppers',
    });
 });
```
