Support Forum
Support › Meta Box Conditional Logic › Disable select option
Hello,
I am trying to disable (not hide) a taxonomy option when the file_upload field is empty.
I took a look at the conditional logic plugin https://metabox.io/docs/meta-box-conditional-logic/ but it's not clear to me if this is possible.
I will appreciate any help. Thank you.
Dear woorise,
The plugin is intended to show or hide field or meta box, it cannot disable a field.
Best,
Tan
Thank you for the quick respond.
Is possible to add the "disable" property in the toggle_type option?
If not how can I check if the file_upload is empty or not when the file_upload is change?
I tried the following but it doesn't work properly:
jQuery('.rwmb-file_upload').on( 'change', function() {
var uploaded = jQuery('.rwmb-media-input').val();
if ( uploaded ) {
//code here
}
});
Dear woorie,
Because toggle_type is affected to whole field/meta box and its container and many fields aren't normal form input field so it isn't possible. Sorry for that.
I see. So I need to do this manual.
Can you please give me some info on what event is triggered when we use the "file_upload" field so I can disable/enable the select option if the user upload a file.
I tried the code above but it doesn’t work properly.
Just to be sure, you want to know when files are added or removed?
@woorise Your solution has a flaw; you check if the result is empty by checking the length. This should work:
jQuery('.rwmb-file_upload').on( 'change', function() {
var uploaded = jQuery('.rwmb-media-input');
if ( uploaded.length ) {
//code here
}
});
I just added a new commit on GitHub. The change event now includes a second argument, which contains all of the hidden inputs. This will allow you to count them to see if any are selected:
jQuery('.rwmb-file_upload').on( 'change', function( event, items) {
if ( items.length ) {
//code here
}
});
Thank you for your answer.
I tried the following on a "select_advanced" field but if I first open the dropdown then it doesn't enable the option.
jQuery(document).ready(function(){
jQuery("#status option[value='23']").prop("disabled", true);
jQuery('.rwmb-file_upload').on( 'change', function( event, items ) {
if ( items.length ) {
jQuery("#status option[value='23']").prop("disabled", false);
}
});
});
Can you post your code so I can experiment with what you are trying to do?