Magento 2 - Create html element with data-mage-init attribute - with Js/Jquery

Magento 2 - Create html element with data-mage-init attribute - with Js/Jquery

July 17, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2 - Create html element with data-mage-init attribute - with Js/Jquery

Magento 2: Adding Elements with data-mage-init Attribute via JavaScript

When developing Magento 2 extensions or customizing your storefront using JavaScript, you might encounter challenges when trying to add elements dynamically while preserving the functionality of attributes like data-mage-init. This attribute is commonly used to initialize Magento’s built-in components and widgets.

The Problem: data-mage-init Attribute Not Parsed via JavaScript

Consider the following scenario:

  1. You have a template that successfully adds an element with the data-mage-init attribute.
  2. When you attempt to replicate this process using JavaScript or jQuery, the newly created elements fail to trigger the intended Magento behavior.

Template-generated item:

<div class="item">
    <div class="item-content">
        <div class="name"><?= $product['name']; ?></div>
        <div class="sku"><?= $product['sku']; ?></div>
        <div class="remove-action">
            <a data-mage-init='{"learningPath": <?= json_encode(['removeUrl' => $block->getUrl('corporate/learning/removeCourse'), 'productId' => $product['id']]) ?>}'
               href="#" class="action secondary"><?= __('Remove'); ?></a>
        </div>
    </div>
</div>

JavaScript-generated item:

$('.path-builder-selection .selection-container').append(`
    <div class='item'>
        <div class='item-content'>
            <div class='name'>${data['product']['name']}</div>
            <div class='sku'>${data['product']['sku']}</div>
            <div class='remove-action'><a data-mage-init='{"learningPath": {"removeUrl": ${data['remove_url']}, "productId": ${data['product']['id']}}}' href='#' class='action secondary'>${data['remove_label']}</a></div>
        </div>
    </div>
`);

When the JavaScript-generated item is added to the DOM, the data-mage-init attribute appears in the browser’s developer tools but fails to initialize the Magento learningPath widget.

The Solution: Using Magento’s Widget Initialization Function

Magento provides an initialization function named Magento_Theme/js/theme. This function is responsible for parsing and initializing widgets based on data attributes found in the DOM. To resolve the issue, you can call this function explicitly after adding your dynamic elements to ensure that they are properly initialized.

Step 1: Require the theme.js file

Ensure that you have required the Magento_Theme/js/theme file in your JavaScript file:

require(['jquery', 'Magento_Theme/js/theme']);

Step 2: Initialize widgets after adding dynamic elements

After appending your dynamic elements to the DOM, call the initialize() function from the Magento_Theme/js/theme namespace. This will ensure that the data-mage-init attributes are parsed and the corresponding widget is initialized.

$('.path-builder-selection .selection-container')
    .append(`
        <div class='item'>
            <div class='item-content'>
                ...
                <div class='remove-action'><a data-mage-init='{"learningPath": {"removeUrl": ${data['remove_url']}, "productId": ${data['product']['id']}}}' href='#' class='action secondary'>${data['remove_label']}</a></div>
            </div>
        </div>
    `)
    .trigger('contentUpdated');

The trigger('contentUpdated') event is used to notify Magento that the content has been updated, prompting it to reinitialize any affected widgets.

Conclusion

When working with dynamic elements in Magento 2, ensure that you require and call the appropriate initialization functions to maintain consistent behavior across both statically rendered and JavaScript-generated content. By following this approach, you can preserve the functionality of attributes like data-mage-init when adding elements via JavaScript or jQuery.

For more information on working with widgets in Magento 2, refer to the official documentation: https://adobe-commerce.redoc.ly/2.4.0-admin/tag/widgets

Happy coding!

Magento 2 Catalog Database Extensions

Our network