Mutual reset: Choice vs. fill-in choice

  • 31 August 2017
  • 7 replies
  • 4 views

Badge +10

Hi community,

I hope that this has already been done by someone:

I have a choice field (drop down) with fill-in possibility:

207168_pastedImage_1.png

Customer wants to have a mutual reset, i.e. if "Individueller Raum" is choosen, the above selection should be reset and vice versa. I guess JavaScript is needed here...

Would anybody be so kind.... happy.png

Cheers

mai-kel


7 replies

Userlevel 5
Badge +14

following script might do the trick

FIChoice is javascript variable of fill-in choice control

NWF.FormFiller.Events.RegisterAfterReady(function () {  

    NWF$('#'+FIChoice).closest('.nf-choice-control-fill-in').find('.nf-choice-control-radio-button-fill,.nf-choice-control-radio-button input[type="radio"]').change(function(evt){
      if (NWF$(evt.target).closest('span').hasClass('nf-choice-control-radio-button-fill')){
           //fill-in radio checked
           var choiceDD=NWF$(evt.target).closest('.nf-choice-control-fill-in').find('select');
           choiceDD.val(choiceDD.find('option[selected="selected"]').val());
      }
      if (NWF$(evt.target).closest('span').hasClass('nf-choice-control-radio-button')){
         //dropdown radio selected
         NWF$(evt.target).closest('.nf-choice-control-fill-in').find('.nf-choice-control-textbox').val('');
      }
    }); 

})‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Badge +10

Uh. I didn't expact that behavior. Adding your code into custom JS section results in the following error message:

207279_pastedImage_1.png

So I needed to take the code out again. sad.png

Userlevel 5
Badge +14

you haven't mentioned you need the script for O365...

this script is for on-prem.

unfortunately I do not have access to an O365 so I can not provide working script. but maybe based on my script you will be able to write one for O365

Userlevel 5
Badge +13

This means that there is an error in the code, like Marian said it was written for On Prem, hence why it might be erroring. You can use the debugger console in your browser to try to find the error.

Badge +3

Hi Marian,

this is perfect code for when you need to track changes user has made and then act on it.

I have a little bit different scenario. I have created JS in the form. It serves to input data into the form if it was copied from already existing item (data is collected by another JS function and put into localStorage).

The form however contains the choice field with fill in choices allowed..here is the script snippet which handles that one.

NWF.FormFiller.Events.RegisterAfterReady(function () {
       if (localStorage.vReasonVal && localStorage.iIntVal && localStorage.pParkVal && localStorage.sSpecsVal && localStorage.rRepeaterContent && localStorage.HowManyRows && localStorage.sStartDateVal && localStorage.eStartDateVal) { 
            var VisitReasonField = NWF$('#' + ddVisitReason); // fill in choice field

            VisitReasonField.val(localStorage.vReasonVal);
            localStorage.removeItem("vReasonVal");
}
});

This all works fine when in the item form before user clicks the button to copy it to new, the drop down fill in choice is selected then data is inputed correctly into the field in copied form.

But if on the original form the fill in choice text box radio button is selected then the value is not copied to the new form. the field stays blank.

I tried to modify your script like

NWF.FormFiller.Events.RegisterAfterReady(function () {
       if (localStorage.vReasonVal && localStorage.iIntVal && localStorage.pParkVal && localStorage.sSpecsVal && localStorage.rRepeaterContent && localStorage.HowManyRows && localStorage.sStartDateVal && localStorage.eStartDateVal) { 

            var VisitReasonField = NWF$('#'+ddVisitReason).closest('.nf-choice-control-fill-in').find('.nf-choice-control-radio-button-fill,.nf-choice-control-radio-button input[type="radio"]');
      if (VisitReasonField.closest('span').hasClass('nf-choice-control-radio-button-fill')){
           var choiceDD=VisitReasonField.closest('.nf-choice-control-fill-in').find('select');
           choiceDD.val(localStorage.vReasonVal);
      }
      if (VisitReasonField.closest('span').hasClass('nf-choice-control-radio-button')){
             VisitReasonField.closest('.nf-choice-control-fill-in').find('.nf-choice-control-textbox').val(localStorage.vReasonVal);
      } 
            localStorage.removeItem("vReasonVal");
}
});

But the field still remains empty when new copied form loads.

Any idea what may be wrong with my code?

Thanx a lot

Jan

Badge +3

Hi Marian (others),

I have solved the problem. Just in case anyone else has the similar issue, so they can do what I did.

First, in JS function (fires on a button click) which saves the original item form data I have the following (just section about handling Fill in Choices field)

ddVisitReason is JS variable name of the Fill in choices enabled field.

var VisitReasonField = NWF$('#' + ddVisitReason);
// find out if drop down select is used for Visit reason
var vRIsDropDownSelected = VisitReasonField.closest('.nf-choice-control-fill-in').find('.nf-choice-control-radio-button input[type="radio"]').prop('checked');
localStorage.setItem("vRIsDropDownSelectedVal", vRIsDropDownSelected);
if (vRIsDropDownSelected == true) {
// if drop down select is used put its value into localStorage
localStorage.setItem("vReasonVal", VisitReasonField.val());
} else {
// if fill im choice text box is used, put its attr "value" into localStorage
var vRFillInTextBox = NWF$('#'+ddVisitReason).closest('.nf-choice-control-fill-in').find('.nf-choice-control-textbox');
localStorage.setItem("vReasonVal", vRFillInTextBox.attr("value"));
}

Then in NWF$(document).ready function I have this to retrieve the value from local storage and put it into newly loaded form

if (localStorage.vRIsDropDownSelectedVal && localStorage.vReasonVal) {
// Visit reason
if (localStorage.vRIsDropDownSelectedVal == "true") {
// dropdown option selected on original form
NWF$('#' + ddVisitReason).val(localStorage.vReasonVal);
} else {
// Fill in choice selected on original form
var vRFillCheckBox = NWF$('#'+ddVisitReason).closest('.nf-choice-control-fill-in').find('.nf-choice-control-radio-button-fill input[type="radio"]');
vRFillCheckBox.click();
var vRFillInTextBox = NWF$('#'+ddVisitReason).closest('.nf-choice-control-fill-in').find('.nf-choice-control-textbox');
vRFillInTextBox.attr("value",localStorage.vReasonVal);
}
// remove localStorage items
localStorage.removeItem("vRIsDropDownSelectedVal");
localStorage.removeItem("vReasonVal");
}

So, I hope this will help someone should they need it.

Thanx again for pointing me at the right direction

Jan

Userlevel 5
Badge +14

great you found a solution.

thanks for sharing.

Reply