How to Make Scripts Run On Button Click
Video Tutorial:
Article:
Adding scripts gives your site extra functionality. When you add a script at page level, you can add a button users can click to activate that script. Here’s how.
- In the Page Builder, click on Page in the top toolbar, then click on Custom Scripts.
- Click the Page tab, then click Add Page Script. Give your script a name (this is important: it allows your script to be targeted by Actions and Events).
- Add a description, and switch the script runs on page load toggle to Off.
- Next, choose the script position. If you are unsure where to put it, choose Body.
- Paste the code of your script into the box, and click Save.
Your script will be added, and a small red indicator will remind you that you need to republish your page(s) before you see the changes.
Next, we’ll add a button and make it trigger the script when clicked.
- In the Elements tray, click Form Elements and drag a Submit button onto your page. In the Button Settings tab of the Properties panel, set the button mode to Trigger.
- After that, click the Add an Action button.
- Then, click Add New Action
- Set up a new action with the following settings:
a. Event > Click > On click
b. Action > Script > Run target script
c. Target > Choose your script from the dropdown list
Click Done.
Now when you publish this page, a click on this button will run your script.