Advertisement
kyled

Gravity Forms Conditional Required Fields Javascript

Apr 3rd, 2012
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 2.28 KB | None | 0 0
  1. // 
  2. // setup required fields.  Variables set as:
  3. // initial_question, followup_question
  4. // Note: This has not been tested with Ajax enabled, so it still requires
  5. // a check on the server side.
  6. //
  7.  
  8. var $q8  = jQuery('li#field_2_16'), $q9  = jQuery('li#field_2_25'),
  9.     $q10 = jQuery('li#field_2_18'), $q11 = jQuery('li#field_2_30'),
  10.     $q12 = jQuery('li#field_2_20'), $q13 = jQuery('li#field_2_21'),
  11.     $q14 = jQuery('li#field_2_22'), $q15 = jQuery('li#field_2_23'),
  12.     requiredAsterisk = '<span class="gfield_required">*</span>';   
  13.  
  14. //
  15. // check for change in field, and mark updateField required if triggerChoice is checked
  16. //
  17. function checkField(triggerChoice, $updateField) {
  18.     // check to see if the triggerChoice input is checked
  19.     if ( jQuery(triggerChoice).attr("checked") === 'checked' ) { // is checked
  20.         // mark the updateField as required if it isn't already
  21.         if ( $updateField.find("span.gfield_required").length == 0 ) {
  22.             $updateField.find("label").eq(0).append(requiredAsterisk);
  23.         }
  24.     }
  25.     else { // not checked
  26.         // we remove the required asterisk since the trigger is not set
  27.         $updateField.find("span.gfield_required").remove();
  28.     }
  29.     return;
  30. }
  31.  
  32. //
  33. // this looks for a change on the initial question's input field
  34. // once its found, it calls the checkField function.  It takes two fields:
  35. //
  36. // triggerChoice : the selector for the input field of the option/choice in
  37. // the initial question that makes the followup question now required
  38. //
  39. // $updateField : This is the field will get updated and set as required if triggerChoice
  40. // is checked.
  41. //
  42.  
  43. $q8.find('input').on('change', function() { checkField('input#choice_16_1', $q9); } );
  44. $q10.find('input').on('change', function() { checkField('input#choice_18_1', $q11); } );       
  45. $q12.find('input').on('change', function() { checkField('input#choice_20_4', $q13); } );       
  46. $q14.find('input').on('change', function() { checkField('input#choice_22_4', $q15); } );           
  47.  
  48. //
  49. // Since the fields might still be required after an attempted page submit, we do a check  once the page
  50. // has loaded, these checkField calls are identical to the ones in the four lines above.
  51. //
  52. checkField('input#choice_16_1', $q9);
  53. checkField('input#choice_18_1', $q11);
  54. checkField('input#choice_20_4', $q13);
  55. checkField('input#choice_22_4', $q15);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement