6

Lets say I want to get ajax response containing everything that is inside #maincontent selector on catalog/product/view/id/1 page.

The problem is that many JS (that works on product view page) doesn't work when I append ajax response html to my custom page (lets say my custom page is some category view page).

I noticed that scripts (from ajax response html) inside doesnt work. like product gallery. How to make the scripts work? I noticed that after I add a product (using add to cart button from the response) the JS in start to work - gallery becomes visible, related products show up, tabs working etc.

KAndy
21k3 gold badges51 silver badges59 bronze badges
asked Dec 3, 2015 at 12:13

1 Answer 1

20

It is very easy to solve.

Look in this line: https://github.com/magento/magento2/blob/develop/lib/web/mage/mage.js#L93

Here mage waiting when you insert html content with ajax: "text/x-magento-init".

Example:

you insert to

<div id="gallery"></div>
$('#gallery').html(content)

After call magento:

$('#gallery').trigger('contentUpdated');
7ochem
7,61516 gold badges54 silver badges82 bronze badges
answered Dec 17, 2015 at 11:03
7
  • this makes mage re-evaluate the whole page. what you want is mage to look only into the specified div. is it possible? Commented Jul 27, 2016 at 10:08
  • I have to use this with products load by infinite ajax scroll. I am using this github.com/manishjoy/magento2-catalog-infinite-scroll but after scroll add to cart button is not working and reload page. Commented Sep 8, 2017 at 13:31
  • 2
    @Magecode, did you got the solution, I am also facing same issue Commented Feb 4, 2019 at 14:29
  • 2
    I am also facing same issue. Any one have a solution? Commented Jul 15, 2021 at 6:41
  • $('#gallery').trigger('contentUpdated'); not working Commented Mar 21, 2022 at 13:22

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.