You can add JavaScript to your own widgets in the widget creator, for example to make an element react to a click. The code runs on every page where you use the widget.
Step 1
In your WordPress dashboard, go to Unlimited Elements > Widgets and open your widget in the widget creator. If you don’t have a widget yet, see How to create your first Elementor widget.
Step 2
Open the HTML tab and give the main element of the widget the id {{uc_id}}. Every copy of the widget on a page gets its own id, so your code works only on its own widget. In this example, button_text is a Text Field attribute of the widget:
Step 3
Open the Javascript tab and write your code. You can use attributes and variables here too, for example {{uc_id}} to find the widget:
jQuery(document).ready(function(){
var objWidget = jQuery("#{{uc_id}}");
objWidget.find(".my-widget__button").on("click", function(){
objWidget.toggleClass("is-active");
});
});
Step 4
If your code uses jQuery or another library, open the js/css Includes tab. Under Libraries, select the library, for example jQuery. To add your own file, use Js Includes or Css Includes.
Click Update to save the widget.