---
metadata:
  - name: generator
    content: Diplodoc Platform v5.70.0
alternate:
  - https://divkit.tech/docs/en/concepts/triggers.md
  - https://divkit.tech/docs/ru/concepts/triggers.md
  - href: https://divkit.tech/docs/en/concepts/triggers.md
    type: text/markdown
    title: Markdown version
  - href: https://divkit.tech/docs/en/llms.txt?revision=r22102947
    rel: describedby
---
> **Documentation Index:** Fetch the complete configuration index at https://divkit.tech/docs/en/llms.txt?revision=r22102947

# Triggers

A trigger runs [actions](https://divkit.tech/docs/en/concepts/interaction.md) when the value of a [variable](https://divkit.tech/docs/en/concepts/variables.md) changes and the specified condition is met. Triggers are declared at the card level and at the level of an individual element. All trigger parameters are listed in the [reference](https://divkit.tech/docs/en/concepts/divs/2/div-trigger.md).

## Card triggers {#variable-triggers}

When changing the variable value (except for updating all properties where the variable is used), any number of [actions](https://divkit.tech/docs/en/concepts/interaction.md) can be run. Add the trigger description in `variable_triggers`:

- `condition`: A condition for running the action. It can contain a boolean expression using variables or trigger events.
- `mode`: Determines when the action triggers:

    - `on_condition`: The action is triggered when a change in the variable results in a certain condition being met (the condition wasn't met until the action occurred).
    - `on_variable`: The action is triggered every time the condition is met when the value of the variable changes.

    For example, suppose the variables change several times, with `condition` returning `true → true → false → true`. When using `on_condition`, the action triggers two times, while with `on_variable` it triggers three times.

- `actions`: Describes [actions](https://divkit.tech/docs/en/concepts/interaction.md) that must be run when the condition is met.

Limitations:

- Conditions that don't contain variables (such as `"condition": "@{1 == 1}"`) or contain functions that don't depend on variables **won't work**, because variables determine when to run the condition check.
- The trigger won't start if the compared entities in the condition have different types (for example, if a boolean variable is compared with an `integer` variable).

For example:

```json translate=no
{
    "states": { ... },
    "variables": { ... },
    "variable_triggers": [
        {
            "condition": "@{liked}",
            "actions": [
                {
                    "url": "div-action://set_variable?name=total_likes&value=@{sum(total_likes, 1)}"
                }
            ]
        },
        {
            "condition": "@{subscribed && !liked}",
            "mode": "on_condition",
            "actions": [
                {
                    "url": "div-action://set_state?state_id=0/subscriptions/expanded"
                },
                {
                    "log_id": "common_posts_shown",
                    "url": "div-action://set_state?state_id=0/common_posts/collapsed"
                }
            ]
        },
        {
            "condition": "@{total_likes > 100 || user_name == 'John'}",
            "mode": "on_variable",
            "actions": [ ... ]
        }
  ]
}
```
In the example above, the first trigger runs the action that increases the `total_likes` variable value each time the `liked` variable value changes from `false` to `true`. The second trigger follows the same logic.

Because of the `"mode": "on_variable"` parameter, the third trigger fires every time the variable values are changed and the `condition` is met.

## Element-level triggers {#local-triggers}

Element-level triggers are activated when the value of an [element-level variable](https://divkit.tech/docs/en/concepts/variables.md#local) changes. Their format is the same as for [card triggers](#variable-triggers).

An element-level trigger:

* has access to element-level variables of the parent object;

* is triggered even for invisible elements (with the parameter `"visibility" = "gone"/"invisible"`);

* is not triggered in states and tabs that are not currently active;

* when used within [item_builder](https://divkit.tech/docs/en/concepts/item_builder.md) prototypes, has access to the current item data through the variable specified in `data_element_name` and the current item index through the `index` variable.

When transitioning to a new state or another tab:

* if the state (tab) is activated for the first time, the trigger fires when the condition in `condition` is met;

* if the state (tab) has already been initialized and the condition was not met before exiting it, the trigger will fire when the condition starts to be met.

<!-- source: en/_includes/index/troubleshooting.md -->
## Learn more {#troubleshooting}

You can discuss topics of interest in the DivKit user community in Telegram: [https://t.me/divkit_community_en](https://t.me/divkit_community_en).



[DivKit Repository](https://github.com/divkit/divkit)
<!-- endsource: en/_includes/index/troubleshooting.md -->
