4

i need to hide form field on on change in magento admin. I need to know that how can i show hide field with his labels in magento admin form my form

$fieldset->addField('style_content', 'select', array(
            'label' => Mage::helper('magazine')->__('Select available Slider Styles'),
            'name' => 'style_content',           
            'values' => array(
                array(
                    'value' => 'Yes',
                    'label' => Mage::helper('magazine')->__('Yes'),
                ),
                array(
                    'value' => 'No',
                    'label' => Mage::helper('magazine')->__('No'),
                ),
            ),
            'onchange'  => 'onchangeStyleShow()',
        ));
Amit Bera
  • 77,456
  • 20
  • 123
  • 237
user3623126
  • 123
  • 1
  • 9

2 Answers2

5

magento is provide to add JavaScript function on admin form using setAfterElementHtml where you can right your custom script.

$field->setAfterElementHtml('<script>
 function onchangeStyleShow(){
// here goes your custom Javascript
}
</script>');
Mukesh Chapagain
  • 5,383
  • 4
  • 37
  • 51
Amit Bera
  • 77,456
  • 20
  • 123
  • 237
  • please answer this question http://magento.stackexchange.com/questions/45809/how-to-create-code-pool-in-magento – user3623126 Nov 29 '14 at 16:32
4

Detail code using setAfterElementHtml:

// This is the field to hide
$fieldset->addField('field_to_hide', text, array(
    'label'  => $this->__('Test'),
));

// Select field
$styleContentField = $fieldset->addField('style_content', 'select', array(
            'label' => Mage::helper('magazine')->__('Select available Slider Styles'),
            'name' => 'style_content',           
            'values' => array(
                array(
                    'value' => 'Yes',
                    'label' => Mage::helper('magazine')->__('Yes'),
                ),
                array(
                    'value' => 'No',
                    'label' => Mage::helper('magazine')->__('No'),
                ),
            ),
            'onchange'  => 'onchangeStyleShow()',
        ));

// Javascript function to show/hide field_to_hide
$styleContentField->setAfterElementHtml('
                        <script>
                        function onchangeStyleShow() {                                              
                            $("field_to_hide").toggle()
                        }
                        </script>
                    ');

More details: Magento: Show/Hide Admin Form fields using Javascript

Mukesh Chapagain
  • 5,383
  • 4
  • 37
  • 51