Adding OnClick Event For Elementor Widgets Through Element Pack

  • BdThemes
  • Adding OnClick Event For Elementor Widgets Through Element Pack

Table Of Contents

Adding OnClick Event For Elementor Widgets Through Element Pack

In this document, we’ll cover:

  1. Setting an ID or class in an Elementor widget.
  2. Writing JavaScript to handle the onClick event.

Setting an ID or Class for an Elementor Widget

First, let’s add an ID or class to the Elementor widget so that we can target it with JavaScript.

Within this tutorial, we will work with Vanilla JS with no dependency.

Steps to Add ID or Class in Elementor:

  1. Open Elementor Editor: Edit the page or post using Elementor.
  2. Select the Widget: Click on the widget you want to add an ID or class to (e.g., a Button or Image widget).
  3. Go to the Advanced Tab:
    – In the widget’s settings, navigate to the Advanced tab.
  4. Add ID or Class:
    CSS ID: Enter a unique ID in the CSS ID field, such as custom-button.
    CSS Classes: Alternatively, add a class name in the CSS Classes field, such as my-button-class.
  5. Example:
    CSS ID: custom-button
    CSS Classes: my-button-class
  6. Update the Page: Save your changes and update the page.

AD 4nXfQ A37r426w81mBzOrQpei3O7M9twLgzw Uk82qCoGR1B36FXbW XbxGKCeZmbTIizCDYyqGq7HanEnlnBO xbEhYdw 7OUn O9IqUTWA g59w267knE Yp JaiwcX7WZaltZ0I7kpFsBDKw0C4poeQk?key=fpmrTvf4rG8XspAfmjfHz6 5 - BdThemes

Ensuring the Custom CSS/JS Feature is Enabled

To add custom JavaScript, ensure that the Custom CSS/JavaScript feature is enabled in Elementor.

AD 4nXdWwuOObPvexRT1 0 iKPlo vPNukna XEmPT92iJOqmXbBiByvbaf2D38fZOjlCAFQtupobYFK87ZlA1Y0sGHkejaXEhtznwjhTN0ybDPScv7oc0IDk06BqajU frzH1vpI8mifPOU zqKGFRDkdHHfG7?key=fpmrTvf4rG8XspAfmjfHz6 5 - BdThemes

Adding JavaScript Code in the Page Settings

After setting up your ID or class, you can now add JavaScript to handle the onClick event.

Adding JavaScript Code in the Page Settings

  1. Go to Page Settings: In the Elementor editor, open the Page Settings by clicking on the gear icon in the bottom left corner.
  2. Navigate to the Advanced Tab: In the Advanced tab of Page Settings, locate the Custom CSS/JavaScript section.
  3. Add the JavaScript Code: Copy and paste the JavaScript code below into the Custom JavaScript section:

AD 4nXdF xlSf4Ve8Qc HGoP sO1PjnJVIK5kHXLmjJKSEFBjudpLuYeT rxDVpSHjXEVUSNU8s DTuyVQA14PCQlsNONvG7P5sL6MOY5i7 mvIK2AExLAdI3Vv GjhiIhTb8iMe HNeaop0VvCc mdNUZiH43Ng?key=fpmrTvf4rG8XspAfmjfHz6 5 - BdThemes

AD 4nXebcHI6uPrz32KLQg0cd6NVz8VT6pqkEXxP3Khy74swS2TmBUsYpPKFrl4y9o0 mei8B7Aw1VGSUZhMny8oLn0gKi31Y3s GWZ68Vb6duz2dAsecNFPs47iH0sPsEVW6IESfgmWvopbTldYVq4 93gicHsW?key=fpmrTvf4rG8XspAfmjfHz6 5 - BdThemes

4. Update or Publish the Page: Save and update/publish the page to apply your changes.

<script>

    document.addEventListener(‘DOMContentLoaded’, function() {

    // Select both ID and class elements using querySelectorAll

    const buttons = document.querySelectorAll(‘#custom-button’);

    // Loop through each selected element and add the onclick event

    buttons.forEach(button => {

        button.onclick = function() {

            alert(‘Button clicked!’);

            // you can call functions also.

        };

    });

});

</script>

Testing

Once the page is updated, preview it in the frontend. Click the button or element with the assigned ID/class to verify if the onClick event is working as expected.

AD 4nXc Bjy9tSEVMygMNVhoRxh2YSd3mvzLDYz4Y srFcLvIlpfjgjCe ZaCZ7T28Zzl4Y2nIHP9CdRToLfhphO9Tiw7n27eR6ziTUS6yWwGTUCyCwiAJheoOyll0hHe2STy - BdThemes

And that’s it! You should now have a fully functioning onClick event for your Elementor widget.

Take advantage of fine-tuned plugins

Get Customizable Elementor Widgets to Power Up Your Website

Take advantage of fine-tuned plugins to speed up web projects without sacrificing quality. We offer a 14-days money-back guarantee.
Need any further assistance? Call Us:+880 1700 55 95 95
Our supported payment system and security badge