Search code examples
jqueryhidden-field

Trying to set attr to disabled when a textfield is hidden?


I am using the jquery-interdepencies library with a form and i like the hidden textfields and/or textfield without a value to not being sent/handled on submit. To accomplish this i have to set disabled to true

$('#testform :input:hidden').attr('disabled', true);

I guess the jquery-interdepencies lib prevents this, but i can't see how i can fix this. I think jquery-interdepencies set the input fields to display: none

btw: how important is it to have hidden or empty textfield not being sent to a form handler

jqyery lib link


Solution

  • According to the docs for this plug-in, you can add callbacks for when you show or hide elements. So something like this will work:

    var cfg = {
        hide: function(elem) {
            elem.hide();
            elem.find('input').attr('disabled','disabled');
        },
        show: function(elem) {
            elem.show();
            elem.find('input').attr('disabled',false);
        }
    };
    
    // Make the ruleset effective on the whole page
    ruleset.install(cfg);
    

    Problem: when I tested this out, I found a bug in the plugin which I have raised on github.

    So you can copy/paste the entire fixed plugin from this fiddle if you wish. It demos how the elements are disabled when hidden.