CYBER MONDAY - 30% OFF Use coupon CM30 at checkout - Learn more

Run JavaScript on Form Load

Using Your Own JavaScript

If you need to run JavaScript when a form loads, you can use the wsf-rendered WS Form event. This will ensure that the form has loaded and rendered fully before your JavaScript runs.

The wsf-rendered event fired on the document object and is passed the form object, form_id and instance_id. You can use these parameters to determine whether your JavaScript should run.

A simple example of running JavaScript on form load is shown below:

(function($) {

    // Create wsf-rendered event handler
    $(document).on('wsf-rendered', function(e, form, form_id, instance_id) {

        // The form ID this script should run for
        var my_form_id = 1;

        // Do not run this JavaScript if this event does not belong to my form ID
        if(form_id != my_form_id) { return; }

        // My JavaScript here ...

    });

})(jQuery);

If you have multiple instances of the same form on a page, the instance_id parameter can be used to differentiate them. This starts with an integer of 1 for the first instance.

Using Conditional Logic

You can also run JavaScript using conditional logic. And example of doing this is shown below:

WS Form - Run JavaScript on Form Load

An advantage of using this method is that you can use WS Form variables in your Javascript.