On change running when form is loaded

I'm having a similar problem to Item change event gets triggered on Save - Forms / SharePoint Online - Plumsail Community

I have three on change events in spRendered, two of which point at the same function, and the functions that they are pointed to are also run at startup.

When they are run at startup, they are passed a parameter which suppresses alerts.

When they run from the on change events, a parameter is passed which allows the alerts.

The problem is that while the non-alert functions run fine, the on change events also run (without any change having happened), and run the functions with the parameter which shows the alerts.

Code snippet from spRendered is below. Am I doing something wrong?

Regards
Nick

doBoth(0);

fd.field('ProjectFrameworkType').$on('change', doBoth(99));
fd.field('CurrentStage').$on('change', getStageNumber(10));
fd.field('NPDStage').$on('change', getStageNumber(20));

function doBoth(showAlert){
    swapFieldsNPD(showAlert);
    getStageNumber(30);
}

function swapFieldsNPD(alertOn) {
    alert(alertOn);
    if(fd.field('ProjectFrameworkType').value == "NPD Project"){
        if(alertOn == 99){
            alert("This an NPD Project");
        }
        fd.field('CurrentStage').disabled = true;
        fd.field('NPDStage').disabled = false;
    }else{
        if(alertOn == 99){
            alert("This is not an NPD Project");
        }
        fd.field('CurrentStage').disabled = false;
        fd.field('NPDStage').disabled = true;
    }
}

Hello @Nick.Jonesm

Please export the form and share it with me, I'll test it on my side.

Hi @mnikitina

Attached is edit form, let me know if you need anything else.

Thanks
Nick
Item_Edit.xfds (118.1 KB)

Hi @mnikitina

I've tried changing the on change lines to this format:

fd.field('ProjectFrameworkType').$on('change', function(value) {doBoth(99)});

And it appears to be behaving as it should. Not sure why.

Regards
Nick

@Nick.Jones,

Yes, when calling the function with the parameter on field change you must call it like this:

fd.field('ProjectFrameworkType').$on('change', function(value) {doBoth(99)})

When calling function without the parameter, you can do it like so:

fd.field('ProjectFrameworkType').$on('change', doBoth);
2 Likes