How to change comments data with onchange function in addfield in magento

How to change comments data with onchange function in addfield in magento

September 4, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to change comments data with onchange function in addfield in magento

Solving Comment Data Changes with onchange Function for Magento AddField

Hello, fellow Magento developers! I hope this post finds you well and ready to dive into an interesting challenge involving custom modules and data manipulation using the onchange function.

The Problem

Our user has created a custom module and is currently facing an issue with updating comments in the admin edit form. They’ve added a new field of type “select”, but they want to dynamically change the comments based on the selected option from this field. The provided code shows that the onchange function is being called, but the values are not being reflected correctly in the comments area.

Here’s the relevant portion of their code:

$eventElem = $fieldset->addField('banner_type', 'select', array(
    ...
    'onchange' => 'checkSelectedItem(this.value)',
    ...
));
$eventElem->setAfterElementHtml("<script type=\"text/javascript\">function checkSelectedItem(selectElement){}</script>");

The Solution

The primary issue lies in how the JavaScript function is being called and how it’s supposed to interact with the Magento admin form. To solve this, we need to ensure that our custom JavaScript function communicates properly with the backend.

First, let me clarify a common misconception: the onchange attribute only triggers when the element loses focus or when the user selects an option from a dropdown. It doesn’t directly update other elements on the page like the comments field.

To achieve this, we need to use Prototype.js (which comes with Magento) and update the form’s fields using its methods. Here’s how you can modify your code:

$eventElem = $fieldset->addField('banner_type', 'select', array(
    ...
    'onchange' => "checkSelectedItem(this.value)",
    ...
));
$eventElem->setAfterElementHtml("<script type=\"text/javascript\">function checkSelectedItem(selectValue){}</script>");

And in your JavaScript file (assuming you have a custom script loaded after the Prototype.js library), add the following code:

function checkSelectedItem(value) {
    var commentField = $('comment_field_id'); // Replace with your actual field ID

    if (value == 'Banner 1') {
        commentField.value = 'This is a Banner 1 comment.';
    } else if (value == 'Banner 2') {
        commentField.value = 'This is a Banner 2 comment.';
    }
}

Best Practices and Further Reading

Remember to always include your custom JavaScript files in the app/design/frontend/<package>/<theme>/layout/local.xml or app/etc/modules/Your_Module.xml file to ensure they’re loaded correctly.

For more detailed information on working with Magento’s admin forms and Prototype.js, I recommend checking out our comprehensive guide on Magento JavaScript development.

Conclusion

In this post, we explored how to update comment data based on a select field’s option change in a custom Magento module. By leveraging Magento’s built-in Prototype.js library and using the onchange function correctly, you can create dynamic and interactive admin forms.

Remember that the key lies in properly communicating between your JavaScript functions and the form elements using Prototype’s methods like $() for selecting elements by ID.

I hope this solution helps you overcome your challenge! If you have any other questions or need further clarification on Magento development, feel free to reach out. Happy coding!

Originally published at Laravel Company by [Your Name], a senior developer and technical blogger.

API Extensions Admin Errors

Our network