Is there a way to call a Javascript function on a button click?

This seems like a pretty basic question, but I can’t see how to do it.

I just want a way to invoke a JS function (in header) when a button is clicked. Right now, I don’t see how to do that using a workflow action for the click event. I suppose I can define my own HTML button (instead of the bubble button component). But, wondering if I’m missing something.

Thanks
Srini

Check out the toolbox plugin by mishav.

1 Like

Hooking up JS code

  • Install the "Toolbox" plugin, drag a button element to the page. In the workflow of that button add the newly available action "Run Javascript" and type in your Javascript. Finish with a call to "bubble_fn_a" passing your result e.g. function fred() { return "hi there fred" } bubble_fn_a(fred())
  • Drag a "JavascriptToBubble" element onto the page. Enter in the suffix "a" (or whatever) which will create the function "bubble_fn_a" being called in the previous step. Check "publish value" and set the "Value Type" to text.
  • Use a text UI element (or whatever) to visualise the result - set the text value to be "dynamic data" and refer to the JavascriptToBubble object's value

Notes

  • The button's action's Javascript contains all the substantive Javascript code.
  • The "JavascriptToBubble" element is only needed to expose the return value of your code computations to the Bubble system. The "JavascriptToBubble" element creates a javascript function that must be called by your code, passing the computation result. The name of the function is "bubble_fn_SUFFIX" where you supply the SUFFIX as anything you want when you configure the "JavascriptToBubble" element.
  • The text UI element (or whatever) can now "see" the result of the Javascript code in your button, by referring to the "JavascriptToBubble" element.

It's a bit convoluted but hopefully this helps. I'm just a beginner on Bubble so hopefully haven't misrepresented anything - Andy.

5 Likes

Hi, I am trying to embed this code to a button which is provided by a 3rd party service. This generates a button in html. How do I add this to a Bubble button element? I am new to programming with Bubble.

Click me to show the form!

There’s also an additional remarks from the provider:
If you would like to programmatically trigger the popup, you can do so by including the script, and calling the public function Paperform.popup(‘2q1ocwbd’); in JavaScript.

I hope you can assist me.

1 Like

@abulka My JS codes have multiple functions. Should I use one suffix e.g. ‘a’ for all? Or each function has own suffix?