AEM show/hide enable/disable dialog fields based on Checkbox

Hello All,

While developing AEM components we might need to show/hide or enable/disable fields in dialog based on Checkbox or on Select dropdown.
I implemented this for Touch UI dialog in AEM 6.5.
Thanks to my friend Sameer Gunjal who helped me here.

Requirement: On the basis of checkbox, We are going to enable/disable textfields of the dialog.


Steps:

 Add Granite:class to the checkbox node and to the fields you want to disable.  Remember, We are going to use these same classes in JS.

 




2     Create a clientlib, to place our JS file in it.
    
        

      I have created a JS file with name listeners.js, 
      Put below JS in the file that you created.
      
(function($) {
   "use strict";
   $(document).on("dialog-ready", function() {
   $(document).on('change', '.checkbox-test', chekbxt);
   if( $('.checkbox-test').length > 0 ) {
      chekbxt();
    }
    function chekbxt (){
        if($(".checkbox-test").attr("checked")){
        $( ".textfield-class" ).prop( "disabled", true );
        }else {$( ".textfield-class" ).prop( "disabled", false );}
       }
   });
})($);

3   Last step, you will have to add the clientlib name in the extraClientlibs property of the dialog.
       

4   Now you will be able to enable/disable fields of the dialog on the selection of checkbox. You can do hide/show similar way, you just need to tweak the JS as per your need.


Comments

Popular posts from this blog

AEM Caching with Google Guava: Use Case, Importance, and Implementation

Creating a Chrome Extension for Easy Redirection to Multiple AEM Instances