Page 1 of 1

dependent fields

Posted: Sun Aug 18, 2019 4:14 pm
by nadobii
Hi,
I write a javascript code in order to make dependent fields in dynaform. but there is a mistake. at the first time, when the user select "No" the "Reasons for disagreement" appears and it's ok.
but when the user select "Yes" and after that (before submit) select "No" the "Reasons for disagreement" and "description" appear!! while the "description" is related to "Yes" selection. json file is attached.
(6.03 KiB) Downloaded 236 times
this is javascript code:
Code: Select all
$("#action_not_approve_description").hide(); //Hide when the form loads
$("#action_teams").hide();
$("#action_plan_draft").hide();
$("#action_approve_description").hide();

$("#action_approve").setOnchange( function(newVal, oldVal) {
   $("#action_not_approve_description").hide();
  $("#action_teams").hide();
  $("action_approve_description").hide();
  $("#action_plan_draft").hide();
        
    
  if (newVal == "1") { 
    $("#action_teams").show();
    $("#action_approve_description").show();
    $("#action_plan_draft").show();
  }

  if (newVal == "0") { 
     $("#action_not_approve_description").show();
         
  }

});
thanks.

Re: dependent fields

Posted: Mon Aug 19, 2019 5:20 pm
by Bosatzu
Hello!

I don't understand what you want to do......but maybe this code help you (i hope).
Code: Select all
load();

function load(){
    
   	$("#action_not_approve_description").hide();
	$("#action_teams").hide();
	$("#action_plan_draft").hide();
	$("#action_approve_description").hide();
	$("#action_approve").setOnchange(hide_show);
}

function hide_show(){

  	if ($("#action_approve").getValue() == "1") {
      	
  	    $("#action_not_approve_description").hide();
      	    $("#action_teams").show();
	    $("#action_approve_description").show();
	    $("#action_plan_draft").show();
    }
    else{
      	
    	    $("#action_teams").hide();
	    $("#action_approve_description").hide();
	    $("#action_plan_draft").hide();
      	    $("#action_not_approve_description").show();
    }
}

Re: dependent fields

Posted: Mon Aug 19, 2019 8:01 pm
by amosbatto
Is this what you want?
(6.18 KiB) Downloaded 258 times

Re: dependent fields

Posted: Mon Aug 19, 2019 9:26 pm
by nadobii
Thanks a lot :)
the first reply is ok.

my problem was as bellow:
the "description" should be appear when the "yes" is selected, but it appears next to the "Reasons for disagreement" when the "No" is selected.
7.png
7.png (66.71 KiB) Viewed 2858 times

Re: dependent fields

Posted: Tue Aug 20, 2019 10:17 pm
by amosbatto
You shouldn't be using a boolean variable for the radio button. You should use a string variable, because you have 3 states: not set, Yes and No. Also, you need to consider that the user might redisplay the form after having already filled out the form.

You should make your form something like this:
(3.69 KiB) Downloaded 243 times
The code is:
Code: Select all
function showOrHideFromAction(decision, oldVal) {    
  if (decision == "yes") { 
    $("#description").show();
    $("#action_not_approve_description").hide();
  }
  else if (decision == "no") {
    $("#description").hide();
    $("#action_not_approve_description").show();
  }
  else { //if decision == ""
    $("#description").hide();
    $("#action_not_approve_description").hide();
  }
}

//when radio button changes:
$("#action_approved").setOnchange( showOrHideFromAction );

//execute when form loads:
showOrHideFromAction($("#action_approved").getValue(), '');