Magento backend, capture onclick events on different elements

Magento backend, capture onclick events on different elements

August 19, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento backend, capture onclick events on different elements

Managing onclick and onchange Events for Magento Radio Buttons

Capturing onclick and onchange events in your existing Magento backend module can be achieved by implementing JavaScript within the form elements themselves or using Magento’s built-in Form Helper class. In this post, we’ll explore both methods to help you enable/disable other controls based on selected radio buttons.

Method 1: Implementing Onclick and Onchange Events Directly

You can add the onclick and onchange attributes directly in your form definition array as shown below:

$fieldset->addField('radio_group', 'radios', array(
    'label'     => Mage::helper('banners')->__('Select campaign type'),
    'name'      => 'title',
    'onclick'   => 'disableOtherFields(this)',
    'onchange'  => 'enableOtherFields(this)',
    'value'     => '1',
    //... other values
));

// Add this script to your form element
<script type="text/javascript">
function disableOtherFields(radio) {
    if (radio.value == '1') {
        DisableElement('field2'); // Replace with the id of the field you want to disable
    }
}

function enableOtherFields(radio) {
    if (radio.value == '1') {
        EnableElement('field2'); // Replace with the id of the field you want to enable
    }
}
</script>

Method 2: Using Magento’s Form Helper Class

For a more integrated approach, you can utilize Magento’s built-in Form Helper class to handle form events. This method requires you to create an additional JavaScript file and include it in your form.

  1. Create a new JavaScript file, e.g., banners.js, under your module folder (app/code/local/YourCompany/YourModule/js/banners.js).

  2. Add the following code to handle radio button events:

function bannersRadioGroupChange(formElement) {
    var fieldId = formElement.id; // Get the id of the selected radio button
    if (fieldId == 'radio_group_1') { // Replace 1 with your desired value
        DisableElement('field2'); // Replace with the id of the field you want to disable
    } else {
        EnableElement('field2'); // Replace with the id of the field you want to enable
    }
}
  1. In your form definition, add a formElementId attribute and include an additional JavaScript file:
$fieldset->addField('radio_group', 'radios', array(
    'label'     => Mage::helper('banners')->__('Select campaign type'),
    'name'      => 'title',
    // Add formElementId for each radio button
    'formElementId'  => 'radio_group_1', // Replace with the desired value
    //... other values
));

// Include your JavaScript file in the form
$block->setTemplate('banners/form.phtml');
  1. In your form.phtml template, include the necessary JavaScript:
<script type="text/javascript">
var bannersJs = new VarienJS();
bannersJs.loadFile('<?php echo $this->getJsUrl('banners.js') ?>', 'banners');
</script>

Remember to replace 'field2' in the examples with the actual ID of the field you want to enable or disable based on the selected radio button.

Conclusion

Capturing onclick and onchange events for Magento radio buttons is a common requirement when building custom admin forms. By following either Method 1 (direct implementation) or Method 2 (using Form Helper class), you can easily implement this functionality in your existing backend module.

For more detailed information on working with Magento forms, I recommend visiting the official Magento documentation: https://docs.magento.com/m2/ce/user_guide/system-requirements.html

Additionally, to learn more about Magento development best practices and stay up-to-date with the latest insights, you can explore our blog at https://laravelcompany.com.

Extensions Admin JavaScript CLI

Our network