Forums

executing javascript function in a load event

tshirley 05 Jun, 2021
Hi,

I have a form where a javascript function executes when a field is changed: so, when a user enters a value into Field 1 or Field2, a js function is called as an Event in the Form, performs a calculation and places the result in Field 3, using the On Change trigger. Works well.

However when I populate the form from a database table or external file, the fields are filled but the js function is not run unless the user changes one of the input fields after loading.

I did try the Not empty trigger but this did not work either.

Is there a way that I can get CF6 to execute the js function when the fields are populated from the database? Preferably a way to run it when triggered by the data being loaded into the field.

Thanks

Tim
GreyHead 05 Jun, 2021
Hi Tim,

You probably need to run your function when the page loads. See this StackOverFlow page for several ways to do this.

Bob
tshirley 05 Jun, 2021
Thanks Bob,

It looks like a good way to go, but I am not certain how that translates into a typical CF6 form.

If I wanted to use one of the solutions here, say

window.onload = function ...
How would I do it? In a custom code after the Display Form?

Cheers

Tim
healyhatman 06 Jun, 2021
There's custom JS code blocks in CFv6, and there's a big giant "on page load" checkbox.
Pigna 10 Jun, 2021
HI!

I had the same problem on a CF7 form and the option "on page load" / "add inside domready event" don't work.

So i've put an img at the end of the page with a transparent 1x1 pixel image that call the function:

<img src="images/pxl.png" alt="" onload="myFunction()" />

and it's work.

p.
You need to login to be able to post a reply.