You are viewing limited content. For full access, please sign in.

Question

Question

Using CSS/JS to fill a field

asked on October 11, 2017

I have a Form with a number of Yes/No radiobuttons.  If any of the radiobuttons is "Yes" then I need to set another Radiobutton field to "Yes," else "No.

I borrowed heavily from https://answers.laserfiche.com/questions/75180/Can-you-create-a-test-that-will-auto-score-in-Forms

 

So, I have each of the radio buttons in CSS class radiobutton.  I have the radiobutton for the result with class pf.  For each of the radiobuttons, I have a value of 1 for the "Yes" answer.   I have this code in the form:

$(document).ready(function (){
  	$('.radiobutton').change(function (){
      	var sum = 0
        $('.radiobutton input:checked').each(function)(){
          	sum += Number($(this).val().replace(/V_/g,''));
        });
      
      if (sum >=1) {
        $('.pf input').val("Yes");
      }
     else
       $('.pf input').val("No");
    }
});
})

I believe I have all the parts together.  And Yet, nothing works.

Is there some js genius out there somewhere who can assist?

0 0

Answer

SELECTED ANSWER
replied on October 11, 2017

Hey Mate,

This should get you what you need.

$(document).ready(function (){
  	$('.radiobutton').change(function (){
      	var sum = 0;
        $('.radiobutton input:checked').each(function(){
          	sum += parseInt($(this).val());
        });
      
      if (sum >= 1) {
        $('.pf input[value="1"]').prop("checked",true).change();
      }
     else
     {
       $('.pf input[value="0"]').prop("checked",true).change();
     }
	});
});

 

1 0
replied on October 12, 2017

This is perfect!  Thank you!

0 0

Replies

replied on October 11, 2017

Can't really help you with the sum part but to force a radio button value, try this:

 if (sum >=1) {
      	$('.pf input[value="1"]').prop('checked', true);
 } else {
      	$('.pf input[value="0"]').prop('checked', true);
 }

Where the numbre between the " " is the value you gave them in the field settings (Exemple: 1 for Yes, 0 for No)

I think you miss a ";" at line 15 to close you ready function.

 

Hope it helped a bit

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.