Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Magento2 | Execute data-mage-init and x-magento-init in dynamic content (ajax request)

Execute data-mage-init and x-magento-init in dynamic content (ajax response)

Trigger .trigger('contentUpdated') on the element where dynamic content is injected.

$.ajax({
    url: 'https://www.example.com',
    method: 'POST',
    data: {
        id: '1'
    },
    success: function (data) {
        $('.example-element').html(data)
                             .trigger('contentUpdated')
    }
});
@kweij

This comment has been minimized.

Copy link

@kweij kweij commented Feb 8, 2018

Exactly what I was looking for, first hit. Cool, thanks!

PS: This also works with content generated with Knockout (templates)

@nuwaus

This comment has been minimized.

Copy link

@nuwaus nuwaus commented Jul 3, 2018

Thank you for this snippet, it saved me.

@daniel-ifrim

This comment has been minimized.

Copy link

@daniel-ifrim daniel-ifrim commented Jul 27, 2018

@timneutkens It doesn't seem to work with knockout templates inside data from element.html(data).
The component is initialized. But something like <div data-bind="visible: myFunction()"> is not called in my case. Tried also with ko if comment statement.

@swishhh

This comment has been minimized.

Copy link

@swishhh swishhh commented Nov 2, 2018

i luv u man

@sreeraj555

This comment has been minimized.

Copy link

@sreeraj555 sreeraj555 commented Apr 27, 2019

awesome. saved my time :). Thank you

@pawarrajendra200

This comment has been minimized.

Copy link

@pawarrajendra200 pawarrajendra200 commented Jun 19, 2019

awesome.Thank you for this snippet, it saved me.

@richardcalahan

This comment has been minimized.

Copy link

@richardcalahan richardcalahan commented Jul 19, 2019

bro....you rock

@lc122218679

This comment has been minimized.

Copy link

@lc122218679 lc122218679 commented Jan 16, 2021

friend
you so rock

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment