Skip to content

Instantly share code, notes, and snippets.

@timneutkens
Last active April 15, 2021 12:10
Show Gist options
  • Save timneutkens/004f8ac0f963068fa718e8397877194e to your computer and use it in GitHub Desktop.
Save timneutkens/004f8ac0f963068fa718e8397877194e to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link

nuwaus commented Jul 3, 2018

Thank you for this snippet, it saved me.

@daniel-ifrim
Copy link

@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
Copy link

swishhh commented Nov 2, 2018

i luv u man

@sreeraj555
Copy link

sreeraj555 commented Apr 27, 2019

awesome. saved my time :). Thank you

@pawarrajendra200
Copy link

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

@richardcalahan
Copy link

bro....you rock

@lc122218679
Copy link

friend
you so rock

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