Pimcore guides &amp; How to 2 min read

# Triggering JS Code in the External File via Button layout component

Learn how to connect your JavaScript code created in a separate JS file and call it in a Button Layout Component handler — which gets triggered when you click that button.

Last updated on 29 August, 2025

## Key takeaways

1. For complex logic behind a Pimcore Button Layout Component, call one function from the Handler field instead of pasting all the code there.
2. Place the JavaScript in a separate file loaded through an event listener when Pimcore starts, so the code is available in the admin.
3. Defining a global object such as window.pimcoreButtonClickEvents keeps the click functions for different buttons in one place.
4. The called function receives the button and its container component, giving access to their IDs and configuration.

![Adding layout component](https://factory.dev/wp-content/uploads/2026/11/adding_layout_component.jpg)

With the help of this tutorial, you’ll learn how to connect your JavaScript code created in a separate JS file and call it in a Button Layout Component handler — which gets triggered when you click that button. Keep reading and find out what to do.

## When to Use

Suppose you have a lot of JS logic that needs to trigger when you click a Button Layout Component in Pimcore Admin. If you had just a few lines of code, then it would look good even if you just pasted them into the Handler input field, but if the logic is complex, you might want to call a single function that will handle everything.

## Step-by-Step Solution

First, we need to create our JS file where our logic will be and connect it to an Event Listener so it is loaded when the Pimcore starts. You can refer to our [older tutoria](https://factory.dev/pimcore-knowledge-base/tutorial/extending-admin-fields-wysiwyg) l for a guide on how to do this. In that guide’s first few steps, we explained how to do precisely that.

Now that we have a place for our JS code ready, let’s see what the Button Layout Component looks like. We added one to our class by right-clicking it, and choosing *Add Layout Component* -> *Button*, like this:

![Adding layout component](https://factory.dev/wp-content/uploads/2026/11/adding_layout_component.jpg)

When you add it, it will look like this:

![Pimcore button handler](https://factory.dev/wp-content/uploads/2026/11/pimcore_button_handler.jpg)

You can see that we already got the suggestion for our handler function from Pimcore. Since we don’t want to have much code here, we want to call a single function. Let’s do that by utilizing the *window* object.

Here we created the *pimcoreButtonClickEvents* object that can hold all of our click functions for different buttons. We also created a simple function *testClickEvent*, that will show an alert on click.

In order to trigger that function on click, we need to call it inside the button Handler, as is displayed below:

![Handler function](https://factory.dev/wp-content/uploads/2026/11/handler_function.jpg)

With this, we now have access in our function to both the button component and its container component, so we can find their IDs, configuration, and everything else we might need to know about them.

## Time for Testing

To test it, we can go to an object of the appropriate class (inside which we created this button), and when we click the button, we should see the Hello world! alert. After that, we are ready to start writing the actual logic of the event.

***Do you have more questions or issues while implementing this? Reach out to us; we’ll be happy to help!***

## Related

[Pimcore guides &amp; How to **Pimcore Studio Setup Guide: Backend &amp; Infrastructure for Developers** Step-by-step overview of Pimcore Studio backend setup, including required services, DDEV configuration, bundle installation, indexing, and production considerations. 34 min read](https://factory.dev/pimcore-knowledge-base/tutorial/pimcore-studio-setup-guide-backend-infrastructure) [Pimcore guides &amp; How to **Customizing Pimcore Classification Store: Removing Grid View Duplicates and Simplifying DataHub Imports** Shared Classification Store attributes can clutter Pimcore grid views and make DataHub imports harder to maintain. This article shows how a custom implementation creates one logical attribute view… 10 min read](https://factory.dev/pimcore-knowledge-base/how-to/customizing-pimcore-classification-store) [Pimcore guides &amp; How to **Using named API routes in separated Vue CLI generated Applications** Discover how to use named, and exposed routes via Symfony FOSJsRoutingBundle in separated Vue CLI generated Applications. 2 min read](https://factory.dev/pimcore-knowledge-base/how-to/named-api-routes-in-separated-vue-cli-generated-applications)

---

Source: https://factory.dev/pimcore-knowledge-base/how-to/triggering-js-code-external-file-button-layout-component
