Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Add and remove required attribute based on whether it is visible or hidden

$('#Additional-Attendees').on('change', function() {
  var v = $(this).val() == "No" ? 0 : +$(this).val();

  var after = $(".additional-attendees:eq(" + v + ")").nextAll(".additional-attendees").addBack();
  var before = $(".additional-attendees:eq(" + v + ")").prevAll(".additional-attendees")
  $.each(before, function() {
    $(this).show();
    $('input[id*="First-Name"]',this).prop("required", true)
    $('input[id*="Last-Name"]',this).prop("required", true)
    $('input[id*="Email"]',this).prop("required", true)
  });
  $.each(after, function() {
    $(this).hide();
    $('input[id*="First-Name"]',this).prop("required", false).val("")
    $('input[id*="Last-Name"]',this).prop("required", false).val("")
    $('input[id*="Email"]',this).prop("required", false).val("")
    $('input[type="checkbox"]:checked',this).prop('checked', false).trigger('change');
  });
});
 
PREVIOUS NEXT
Tagged: #Add #remove #required #attribute #based #visible #hidden
ADD COMMENT
Topic
Name
7+9 =