Custom button onclick handler

I have used render_template to make a nice looking button on which I want to call an onclick function.


<button onclick="load_bom()">Load Linked BOM</button>

As usual, I’m being dense about JavaScript scope. How can I get this to call a function in mydoctype.js?

1 Like

check this it might help you .

i usually call python method with js , why do you want to call js method onclick to make a js code while you are already can write the js code

Thanks @ahmadRagheb . I am familiar with and have implemented variations on most of these examples. I plan to execute a callback with onclick, but I can’t seem to get it to find the load_bom() function, which I’m assuming is a scope issue, maybe because the button is rendered in an HTML field. Personally, I’m much more comfortable working in Python but all of the stuff I want to do is in JS. That’s life I guess.

Apparently calling a function from onclick is bad form and one is now encouraged to use something like this:
document.getElementById("load_bom_button").addEventListener("click", load_bom, false);

This code must appear after the button has been rendered.

<button id="load_bom_button">Load Linked BOM</button>


Hello @tmatteson

I’ve got a custom web form, I’m trying to call a function when a button is clicked.

Where do put this part?

Currently the function runs whenever i add this code without clicking the button.

@adam26d A better way to do this is to use idiomatic jQuery. It’s easier and guaranteed to be available in web forms or in desk.

$('#load_bom_button') // query selector for <button id="load_bom_button">
.off() // unbind everything to start so that you're only attaching a single handler
.on('click', () => { // bind a function to the click event
  load_bom() // the action you want the button to execute

Off documentation
On documentation


Thank you, this worked!