Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove required from input fields that are in hidden divs

I have one form and couple of divs with bunch of input fields, and one select field that shows one div and hide all others. In those input field I have html5 required tag. I would like to remove required tag from all input fields that are in hidden divs. How can I do that? Here is my js for show/hiding divs depending on my select options:

    <script>
function changeGroup(e) {
    /*
        1 - Remove all group classes
        2 - Add the currently selected group class
    */
    $("#main")
        .removeClass(allGroups)
        .addClass($("option:selected", e.target).val())     
}

/* 
    1 - Bind changeGroup function to "change" event
    2 - Fetch all group from the select field 
*/
var allGroups = $("select")
    .change(changeGroup)
    .find("option")
        .map(function() {
            return $(this).val();
        })
        .get()
        .join(' ');


</script>

Thank you for help... Dorijan

like image 446
user899119 Avatar asked Nov 05 '13 00:11

user899119


People also ask

How do you remove a required field?

Click Customize the System. Under Components, expand Entities, and then expand the entity you want. Click Fields. Change the Field Requirement from Business Required to Optional.

What are hidden inputs?

<input type="hidden"> <input> elements of type hidden let web developers include data that cannot be seen or modified by users when a form is submitted. For example, the ID of the content that is currently being ordered or edited, or a unique security token.

How to submit hidden input?

The <input type="hidden"> defines a hidden input field. A hidden field lets web developers include data that cannot be seen or modified by users when a form is submitted. A hidden field often stores what database record that needs to be updated when the form is submitted.

Why use hidden input field?

In case of Hidden Form Field a hidden (invisible) textfield is used for maintaining the state of an user. In such case, we store the information in the hidden field and get it from another servlet. This approach is better if we have to submit form in all the pages and we don't want to depend on the browser.


1 Answers

Would use prop() method since required is a property. Once you remove the property then if user toggles so the become visible again have no current mechanism to track where they got removed.

For this reason I suggest you add a class requiredto all the inputs that have required property.

$('.required').prop('required', function(){
   return  $(this).is(':visible');
});

This will loop over all of the class required and adjust property based on visibility

like image 75
charlietfl Avatar answered Oct 05 '22 19:10

charlietfl