Hide fields with "required" attribute (v2023)

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
gbellabarba
User
Posts: 8

Hide fields with "required" attribute (v2023)

Post by gbellabarba »

Hi, I have set some required fields which are hidden based on some choices. initially everything worked automatically but then suddenly when I go to add a record, it started asking me for the required but hidden and empty fields. I set the required field by code but it doesn't work.


arbei
User
Posts: 9719

Post by arbei »

gbellabarba wrote:

I set the required field by code

You may post your code for discussion.


gbellabarba
User
Posts: 8

Post by gbellabarba »

$(document).ready(function() {

    function toggleIdLargeAreaUnocHabitat() {
        $("#r_id_large_area_unoc_habitat").attr("required", "required");;
        if ($("#x_id_area_suf_long_tern").val() == "1" || $("#x_id_area_suf_long_tern").val() == "3") {
            $("#r_id_large_area_unoc_habitat").hide();
            //$("#x_id_large_area_unoc_habitat").prop("required", false);
            $("#x_id_large_area_unoc_habitat").removeAttr("required");
            $("#x_id_large_area_unoc_habitat").val("").trigger('change');
        } else {
            $("#r_id_large_area_unoc_habitat").show();
            //$("#x_id_large_area_unoc_habitat").prop("required", true);
            $("#x_id_large_area_unoc_habitat").attr("required", "required");
        }
    }

    
    toggleIdLargeAreaUnocHabitat();

   $("#x_id_area_suf_long_tern").change(function() {
        toggleIdLargeAreaUnocHabitat();
    });
});

arbei
User
Posts: 9719

Post by arbei »

You JavaScript code should check if the field is hidden before adding the "required" attribute, see :hidden Selector.


Post Reply