Changing required field dynamicly via JS in Contact Form 7

I need to write custom validated form in WordPress. After the click on For-Author radio button some checkboxes group should be marked as not required. I tried to change “aria-required” attributes to “false” or remove and add class “wpcf7-validates-as-required” but it doesn’t work. I also tried to reinit wpcf7 by

$('div.wpcf7 > form').wpcf7InitForm();

but it’s also doesn’t work.

Read More

This is a code sample:

$radio.button.click(function (event) {
    isAuthor(event)
});

var isAuthor = function (event) {
    if ($(event.target).attr('data-author') == 'true') {
        authorClicked = true;
    } else {
        authorClicked = false;
    }
    fieldUpdate();
};

var fieldUpdate = function () {
    var $text = $('[name=pres-title]');
    if (authorClicked) {
        $text.prop('disabled', false).attr('aria-required', true).addClass('wpcf7-validates-as-required').attr('aria-invalid', false)
        $fieldRequired.each(function () {
            $(this).prop('disabled', false).attr('aria-required', true).addClass('wpcf7-validates-as-required').attr('aria-invalid', false);
        });
    } else {
        $text.prop('disabled', true).attr('aria-required', false).removeClass('wpcf7-validates-as-required').attr('aria-invalid', false);
        $fieldRequired.each(function () {
            $(this).prop('disabled', true).attr('aria-required', false).removeClass('wpcf7-validates-as-required').attr('aria-invalid', false);
        });
    }
}

Related posts