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

Question

Question

On Form Open, Check a Radio Button, Set Another Field as Required

asked on May 10, 2017

On form open, if Specification = Yes, I'd like the QAE field to be required.

I have it coded to also set QAE to required when Specification is changed to yes and not required when Specification is changed to no. On change is working fine ... on form open doesn't work. What am I doing wrong? Here's the code:

$(document).ready(function(){
//set QAE to required if specification = yes
  $(".specification input:checked").each(function(){
  $('.specification input').change(function () {
    if ($(this).val() == "Yes")
      {
        $('.qae label').append('<span class="cf-required">*</span>');
        $('.qae input').attr('required', 'True');
      }
    else
      {
        $('.qae .cf-label span.cf-required').remove();
        $('.qae input').removeClass('required').removeAttr('required');
      }
  });
  });
});  //close document.ready

 

0 0

Answer

SELECTED ANSWER
replied on May 10, 2017

So the 'specification' has a default value of 'yes', right?

You can re-organize the code like this:

$(document).ready(function(){
//set QAE to required if specification = yes
  $('.specification input').change(function () {
    if ($(this).val() == "Yes")
      {
        $('.qae label').append('<span class="cf-required">*</span>');
        $('.qae input').attr('required', 'True');
      }
    else
      {
        $('.qae .cf-label span.cf-required').remove();
        $('.qae input').removeClass('required').removeAttr('required');
      }
  });
  $(".specification input:checked").each(function(){  
    $(this).trigger('change');
  });
});  //close document.ready

 

0 0

Replies

replied on May 11, 2017

Thank you! Worked perfectly!

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

Sign in to reply to this post.