Client script both onchange and onload

CE_
Tera Contributor



9 REPLIES 9

Peter Bodelier
Giga Sage

Hi @CE_,

 

A client script cannot work onload and onchange. You will have to create 2 Client scripts.


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

How would these 2 client script look like?

You could use the script provided by @Danish Bhairag2.

 

Just split them between 2 client scripts.
It seems it can work. I wouldn't create it like this though, because you won't be able to see that is also works onload, unless you look at the script. That is not very maintainable.

 

Since you will be reusing the bulk part of the script, it may be a good idea, to use a script include for that part, even though it is not needed.

 

function onLoad() {

   function checkAgreement() {

    var agreementField = g_form.getControl('agreement');

    var agreementValue = agreementField.value;

    

    var message1 = g_form.getMessages('message1');

    var message2 = g_form.getMessages('message2');



    if (agreementValue === 'none') {

        if (message1.length === 0) {

            g_form.addInfoMessage('message1');

        }

        if (message2.length > 0) {

            g_form.removeMessage('message2');

        }

    } else if (agreementValue === 'yes') {

        if (message2.length === 0) {

            g_form.addInfoMessage('message2');

        }

        if (message1.length > 0) {

            g_form.removeMessage('message1');

        }

    } else if (agreementValue === 'no') {

        // If 'no' is chosen, no message is shown

        if (message1.length > 0) {

            g_form.removeMessage('message1');

        }

        if (message2.length > 0) {

            g_form.removeMessage('message2');

        }

    }

}

 

function onChange(control, oldValue, newValue, isLoading) {

   function checkAgreement() {

    var agreementField = g_form.getControl('agreement');

    var agreementValue = agreementField.value;

    

    var message1 = g_form.getMessages('message1');

    var message2 = g_form.getMessages('message2');



    if (agreementValue === 'none') {

        if (message1.length === 0) {

            g_form.addInfoMessage('message1');

        }

        if (message2.length > 0) {

            g_form.removeMessage('message2');

        }

    } else if (agreementValue === 'yes') {

        if (message2.length === 0) {

            g_form.addInfoMessage('message2');

        }

        if (message1.length > 0) {

            g_form.removeMessage('message1');

        }

    } else if (agreementValue === 'no') {

        // If 'no' is chosen, no message is shown

        if (message1.length > 0) {

            g_form.removeMessage('message1');

        }

        if (message2.length > 0) {

            g_form.removeMessage('message2');

        }

    }

}

 

 

 


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Thanks, But does the onLoad script have to be that long? 
None is the onLoad value so then I wouldnt need any if statements I guess?