Forums

radio button (2) paired with required text field (2)

chuanse 24 Mar, 2014
Hi,

Could someone give me a hand please?

There is a question ('statuut') with 2 possible answers (radio button options)

If answer 1 is selected, a required text field ('riziv') should appear, if answer 2 is selected the textbox should disappear
If answer 2 is selected, a required text field ('rolnr') should appear, if answer 1 is selected the textbox should disappear

I am trying to compose the code using examples on this forum, but I can't get it to work :/
The fields are not disappearing/appearing when answer 1 or 2 is selected or changed


var textarea1_id, textarea1, textarea2_id, textarea2, div;
/* edit the next two lines to match the ids of the elements in your form */
textarea1_id = 'riziv';
textarea2_id = 'rolnr';

window.addEvent('domready', function() {
  textarea1 = $(textarea1_id);
  div1 = $(textarea1_id+'_container_div');
  div1.dissolve();
  showHide();
  $('statuut_1').addEvent('change', showHide);
});

window.addEvent('domready', function() {
  textarea2 = $(textarea2_id);
  div2 = $(textarea2_id+'_container_div');
  div2.dissolve();
  showHide();
$('statuut_0').addEvent('change', showHide);
});

function showHide() {
  if ( $('statuut_1').checked === true && $('statuut_1').checked === false ) {
    div1.reveal();
    textarea1.disabled = false;
	div2.dissolve();
	textarea2.disabled = true;
	$('riziv').addClass("validate['required']");
     formCheck.register($('riziv'));
	 textarea2.value = '';
  } elseif ( $('statuut_1').checked === false && $('statuut_1').checked === true ) {
    div2.reveal();
    textarea2.disabled = false;	
    div1.dissolve();
	textarea1.disabled = true;
	$('rolnr').addClass("validate['required']");
     formCheck.register($('rolnr'));
    textarea1.value = '';
  } else {
  div1.dissolve();
  div2.dissolve();
  }
}


any help is very welcome... thanks in advance.

J
GreyHead 24 Mar, 2014
Hi chuanse,

Please post a link to the form so I can take a quick look.

Bob
chuanse 24 Mar, 2014
Hello Greyhead,

I sent the link in pm to you.

Thanks.

J
GreyHead 24 Mar, 2014
Hi chaunse,

There is a JavaScript error on the page that I can't pin down - "Uncaught SyntaxError: Unexpected token { "

And the logic of the two 'if' lines is wrong. The same radio button can never be both checked and unchecked.
if ( $('statuut_1').checked === true && $('statuut_1').checked === false ) {

Bob
chuanse 24 Mar, 2014
Hello Greyhead,

you are right, statuut_1 and statuut_2 are better😉

I got it working now EXCEPT Formcheck
Uncaught ReferenceError: formCheck is not defined... What can I do about that?


var textarea1_id, textarea1, textarea2_id, textarea2, div1, div2;
/* edit the next two lines to match the ids of the elements in your form */
textarea1_id = 'riziv';
textarea2_id = 'rolnr';

window.addEvent('domready', function() {
  textarea1 = $(textarea1_id);
  div1 = $(textarea1_id+'_container_div');
  div1.dissolve();
  
  textarea2 = $(textarea2_id);
  div2 = $(textarea2_id+'_container_div');
  div2.dissolve();
  
  showHide();
  $('statuut_1').addEvent('change', showHide);
  $('statuut_0').addEvent('change', showHide);
});

function showHide() {
 textarea1 = $(textarea1_id);
 textarea2 = $(textarea2_id);
 div1 = $(textarea1_id+'_container_div');
 div2 = $(textarea2_id+'_container_div');
 
  if ( $('statuut_1').checked == true ) {
    div1.reveal();
    textarea1.disabled = false;
	div2.dissolve();
	textarea2.disabled = true;
	$('riziv').addClass("validate['required']");
    formCheck.register($('riziv'));
	textarea2.value = '';
  } else if ( $('statuut_0').checked == true ) {
    div2.reveal();
    textarea2.disabled = false;	
    div1.dissolve();
	textarea1.disabled = true;
	$('rolnr').addClass("validate['required']");
    formCheck.register($('rolnr'));
    textarea1.value = '';
  } else {
  div1.dissolve();
  textarea1.disabled = true;
  div2.dissolve();
  textarea2.disabled = true;
  }
};
GreyHead 24 Mar, 2014
Hi Chaunse,

Please turn off the Dynamic File option in the Load JS action. It delays the loading of the formCheck code.

Bob
chuanse 24 Mar, 2014
Dear Greyhead,

that was not enabled...
chuanse 24 Mar, 2014
Dear Greyhead,

it seems despite the JS error the field validation is working.

One last Question...

In the form there is a question with 2 checkboxes: ppl may click on 1 or both of the checkboxes.

On the thank you page and the confirmation mail the output of these checkboxes is repeated by calling the curly brackets of that multiple choice question.

This returns the output array: how can I convert that (e.g. strip the php symbols)

"Uw inschrijving voor array ( 0 => 'medisfeer', 1 => 'specialist', ) werd geregistreerd."
chuanse 24 Mar, 2014
Nevermind last questions Greyhound.

I found out you already helped me out for the same issue about 2y ago😉

Big thanks!!

Kind regards
J
This topic is locked and no more replies can be posted.