Execute JavaScript code after swatches are displayed in category view












1














I'm trying to trigger custom event whenever Magento displays the product swatches in category view.



It looks like Magento uses ajax to load these swatches, they are not displayed on page load, they "pop" after a few moments. I need to add some custom JavaScript code after these swatches are actually displayed. So I thought the best way will be to trigger custom event just after Magento finishes to render the swatches. But I'm not sure how to do this correctly in Magento 2.



enter image description here



What I want to do is to trigger a custom event $(document).trigger('swatches-visible'); when a block of swatches is finally displayed on the page.



And then, an event handler will execute a function:



$(document).on('swatches-visible', function() { 
doSomething();
});


That function doSomething() is already added via RequireJS in one of the template files:



<script type="text/javascript">
requirejs(['jquery'], function(jQuery) {
function doSomething()
{
// Here make some layout changes of the products list...
}
});
</script>









share|improve this question
























  • Are you loading your script via RequireJS? I ask this as you can tell Require to only load a script when a certain element has been rendered with x-magento-ini. See this section of the dev docs for more info - devdocs.magento.com/guides/v2.0/javascript-dev-guide/javascript/…
    – Ben Crook
    Oct 3 '16 at 12:30










  • @Ben-Space48 I want to trigger custom event $(document).trigger('swatches-visible') when a block of swatches is finally displayed. And then, an event handler will execute a function: $(document).on('swatches-visible', function(e) { doSomething(); }); That function doSomething() is already added via RequireJS in one of the template files. What you proposed sounds like a good solution but I'm not sure how to use it in my case. Isn't
    – zitix
    Oct 3 '16 at 15:08












  • Isn't data-mage-init executed only on page load? Note that the blocks of swatches for all products, e.g. <div class="swatch-opt-1822">...</div>, are displayed with a delay, not instantly on page load.
    – zitix
    Oct 3 '16 at 15:20










  • I know data-mage-init is, x-magento-init is different although I'm not sure if that is also only on page load (the dev docs don't mention). If it is page load then the only other method I can think of is to initialise the JS inside the swatch template. Require works in templates to so I don't see why that wouldn't work.
    – Ben Crook
    Oct 3 '16 at 15:33










  • Although JS inside a template isn't the cleanest method :(
    – Ben Crook
    Oct 3 '16 at 15:34
















1














I'm trying to trigger custom event whenever Magento displays the product swatches in category view.



It looks like Magento uses ajax to load these swatches, they are not displayed on page load, they "pop" after a few moments. I need to add some custom JavaScript code after these swatches are actually displayed. So I thought the best way will be to trigger custom event just after Magento finishes to render the swatches. But I'm not sure how to do this correctly in Magento 2.



enter image description here



What I want to do is to trigger a custom event $(document).trigger('swatches-visible'); when a block of swatches is finally displayed on the page.



And then, an event handler will execute a function:



$(document).on('swatches-visible', function() { 
doSomething();
});


That function doSomething() is already added via RequireJS in one of the template files:



<script type="text/javascript">
requirejs(['jquery'], function(jQuery) {
function doSomething()
{
// Here make some layout changes of the products list...
}
});
</script>









share|improve this question
























  • Are you loading your script via RequireJS? I ask this as you can tell Require to only load a script when a certain element has been rendered with x-magento-ini. See this section of the dev docs for more info - devdocs.magento.com/guides/v2.0/javascript-dev-guide/javascript/…
    – Ben Crook
    Oct 3 '16 at 12:30










  • @Ben-Space48 I want to trigger custom event $(document).trigger('swatches-visible') when a block of swatches is finally displayed. And then, an event handler will execute a function: $(document).on('swatches-visible', function(e) { doSomething(); }); That function doSomething() is already added via RequireJS in one of the template files. What you proposed sounds like a good solution but I'm not sure how to use it in my case. Isn't
    – zitix
    Oct 3 '16 at 15:08












  • Isn't data-mage-init executed only on page load? Note that the blocks of swatches for all products, e.g. <div class="swatch-opt-1822">...</div>, are displayed with a delay, not instantly on page load.
    – zitix
    Oct 3 '16 at 15:20










  • I know data-mage-init is, x-magento-init is different although I'm not sure if that is also only on page load (the dev docs don't mention). If it is page load then the only other method I can think of is to initialise the JS inside the swatch template. Require works in templates to so I don't see why that wouldn't work.
    – Ben Crook
    Oct 3 '16 at 15:33










  • Although JS inside a template isn't the cleanest method :(
    – Ben Crook
    Oct 3 '16 at 15:34














1












1








1







I'm trying to trigger custom event whenever Magento displays the product swatches in category view.



It looks like Magento uses ajax to load these swatches, they are not displayed on page load, they "pop" after a few moments. I need to add some custom JavaScript code after these swatches are actually displayed. So I thought the best way will be to trigger custom event just after Magento finishes to render the swatches. But I'm not sure how to do this correctly in Magento 2.



enter image description here



What I want to do is to trigger a custom event $(document).trigger('swatches-visible'); when a block of swatches is finally displayed on the page.



And then, an event handler will execute a function:



$(document).on('swatches-visible', function() { 
doSomething();
});


That function doSomething() is already added via RequireJS in one of the template files:



<script type="text/javascript">
requirejs(['jquery'], function(jQuery) {
function doSomething()
{
// Here make some layout changes of the products list...
}
});
</script>









share|improve this question















I'm trying to trigger custom event whenever Magento displays the product swatches in category view.



It looks like Magento uses ajax to load these swatches, they are not displayed on page load, they "pop" after a few moments. I need to add some custom JavaScript code after these swatches are actually displayed. So I thought the best way will be to trigger custom event just after Magento finishes to render the swatches. But I'm not sure how to do this correctly in Magento 2.



enter image description here



What I want to do is to trigger a custom event $(document).trigger('swatches-visible'); when a block of swatches is finally displayed on the page.



And then, an event handler will execute a function:



$(document).on('swatches-visible', function() { 
doSomething();
});


That function doSomething() is already added via RequireJS in one of the template files:



<script type="text/javascript">
requirejs(['jquery'], function(jQuery) {
function doSomething()
{
// Here make some layout changes of the products list...
}
});
</script>






magento2 configurable-product swatches category-view






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 3 '16 at 15:25







zitix

















asked Oct 3 '16 at 11:58









zitixzitix

1,04562140




1,04562140












  • Are you loading your script via RequireJS? I ask this as you can tell Require to only load a script when a certain element has been rendered with x-magento-ini. See this section of the dev docs for more info - devdocs.magento.com/guides/v2.0/javascript-dev-guide/javascript/…
    – Ben Crook
    Oct 3 '16 at 12:30










  • @Ben-Space48 I want to trigger custom event $(document).trigger('swatches-visible') when a block of swatches is finally displayed. And then, an event handler will execute a function: $(document).on('swatches-visible', function(e) { doSomething(); }); That function doSomething() is already added via RequireJS in one of the template files. What you proposed sounds like a good solution but I'm not sure how to use it in my case. Isn't
    – zitix
    Oct 3 '16 at 15:08












  • Isn't data-mage-init executed only on page load? Note that the blocks of swatches for all products, e.g. <div class="swatch-opt-1822">...</div>, are displayed with a delay, not instantly on page load.
    – zitix
    Oct 3 '16 at 15:20










  • I know data-mage-init is, x-magento-init is different although I'm not sure if that is also only on page load (the dev docs don't mention). If it is page load then the only other method I can think of is to initialise the JS inside the swatch template. Require works in templates to so I don't see why that wouldn't work.
    – Ben Crook
    Oct 3 '16 at 15:33










  • Although JS inside a template isn't the cleanest method :(
    – Ben Crook
    Oct 3 '16 at 15:34


















  • Are you loading your script via RequireJS? I ask this as you can tell Require to only load a script when a certain element has been rendered with x-magento-ini. See this section of the dev docs for more info - devdocs.magento.com/guides/v2.0/javascript-dev-guide/javascript/…
    – Ben Crook
    Oct 3 '16 at 12:30










  • @Ben-Space48 I want to trigger custom event $(document).trigger('swatches-visible') when a block of swatches is finally displayed. And then, an event handler will execute a function: $(document).on('swatches-visible', function(e) { doSomething(); }); That function doSomething() is already added via RequireJS in one of the template files. What you proposed sounds like a good solution but I'm not sure how to use it in my case. Isn't
    – zitix
    Oct 3 '16 at 15:08












  • Isn't data-mage-init executed only on page load? Note that the blocks of swatches for all products, e.g. <div class="swatch-opt-1822">...</div>, are displayed with a delay, not instantly on page load.
    – zitix
    Oct 3 '16 at 15:20










  • I know data-mage-init is, x-magento-init is different although I'm not sure if that is also only on page load (the dev docs don't mention). If it is page load then the only other method I can think of is to initialise the JS inside the swatch template. Require works in templates to so I don't see why that wouldn't work.
    – Ben Crook
    Oct 3 '16 at 15:33










  • Although JS inside a template isn't the cleanest method :(
    – Ben Crook
    Oct 3 '16 at 15:34
















Are you loading your script via RequireJS? I ask this as you can tell Require to only load a script when a certain element has been rendered with x-magento-ini. See this section of the dev docs for more info - devdocs.magento.com/guides/v2.0/javascript-dev-guide/javascript/…
– Ben Crook
Oct 3 '16 at 12:30




Are you loading your script via RequireJS? I ask this as you can tell Require to only load a script when a certain element has been rendered with x-magento-ini. See this section of the dev docs for more info - devdocs.magento.com/guides/v2.0/javascript-dev-guide/javascript/…
– Ben Crook
Oct 3 '16 at 12:30












@Ben-Space48 I want to trigger custom event $(document).trigger('swatches-visible') when a block of swatches is finally displayed. And then, an event handler will execute a function: $(document).on('swatches-visible', function(e) { doSomething(); }); That function doSomething() is already added via RequireJS in one of the template files. What you proposed sounds like a good solution but I'm not sure how to use it in my case. Isn't
– zitix
Oct 3 '16 at 15:08






@Ben-Space48 I want to trigger custom event $(document).trigger('swatches-visible') when a block of swatches is finally displayed. And then, an event handler will execute a function: $(document).on('swatches-visible', function(e) { doSomething(); }); That function doSomething() is already added via RequireJS in one of the template files. What you proposed sounds like a good solution but I'm not sure how to use it in my case. Isn't
– zitix
Oct 3 '16 at 15:08














Isn't data-mage-init executed only on page load? Note that the blocks of swatches for all products, e.g. <div class="swatch-opt-1822">...</div>, are displayed with a delay, not instantly on page load.
– zitix
Oct 3 '16 at 15:20




Isn't data-mage-init executed only on page load? Note that the blocks of swatches for all products, e.g. <div class="swatch-opt-1822">...</div>, are displayed with a delay, not instantly on page load.
– zitix
Oct 3 '16 at 15:20












I know data-mage-init is, x-magento-init is different although I'm not sure if that is also only on page load (the dev docs don't mention). If it is page load then the only other method I can think of is to initialise the JS inside the swatch template. Require works in templates to so I don't see why that wouldn't work.
– Ben Crook
Oct 3 '16 at 15:33




I know data-mage-init is, x-magento-init is different although I'm not sure if that is also only on page load (the dev docs don't mention). If it is page load then the only other method I can think of is to initialise the JS inside the swatch template. Require works in templates to so I don't see why that wouldn't work.
– Ben Crook
Oct 3 '16 at 15:33












Although JS inside a template isn't the cleanest method :(
– Ben Crook
Oct 3 '16 at 15:34




Although JS inside a template isn't the cleanest method :(
– Ben Crook
Oct 3 '16 at 15:34










2 Answers
2






active

oldest

votes


















5














I used the event 'swatch.initialized' to fire a function after swatches are loaded in the product page:



$(document).on('swatch.initialized', function() {
// do something
})


The event is triggered in Magento_Swatches/js/swatch-renderer.js



$(this.element).trigger('swatch.initialized');





share|improve this answer































    0














    Try With This one.



     <script type="text/javascript">
    require(['jquery','Magento_Swatches/js/swatch-renderer'],function($,swatch){
    $(document).on('swatch.initialized', function() {
    function doSomething()
    {
    // Here make some layout changes of the products list...
    }
    });
    </script>





    share|improve this answer





















      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "479"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f139122%2fexecute-javascript-code-after-swatches-are-displayed-in-category-view%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      5














      I used the event 'swatch.initialized' to fire a function after swatches are loaded in the product page:



      $(document).on('swatch.initialized', function() {
      // do something
      })


      The event is triggered in Magento_Swatches/js/swatch-renderer.js



      $(this.element).trigger('swatch.initialized');





      share|improve this answer




























        5














        I used the event 'swatch.initialized' to fire a function after swatches are loaded in the product page:



        $(document).on('swatch.initialized', function() {
        // do something
        })


        The event is triggered in Magento_Swatches/js/swatch-renderer.js



        $(this.element).trigger('swatch.initialized');





        share|improve this answer


























          5












          5








          5






          I used the event 'swatch.initialized' to fire a function after swatches are loaded in the product page:



          $(document).on('swatch.initialized', function() {
          // do something
          })


          The event is triggered in Magento_Swatches/js/swatch-renderer.js



          $(this.element).trigger('swatch.initialized');





          share|improve this answer














          I used the event 'swatch.initialized' to fire a function after swatches are loaded in the product page:



          $(document).on('swatch.initialized', function() {
          // do something
          })


          The event is triggered in Magento_Swatches/js/swatch-renderer.js



          $(this.element).trigger('swatch.initialized');






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 25 '18 at 16:24









          Piyush

          4,76872053




          4,76872053










          answered Jan 25 '18 at 15:35









          Guillem LormanGuillem Lorman

          5113




          5113

























              0














              Try With This one.



               <script type="text/javascript">
              require(['jquery','Magento_Swatches/js/swatch-renderer'],function($,swatch){
              $(document).on('swatch.initialized', function() {
              function doSomething()
              {
              // Here make some layout changes of the products list...
              }
              });
              </script>





              share|improve this answer


























                0














                Try With This one.



                 <script type="text/javascript">
                require(['jquery','Magento_Swatches/js/swatch-renderer'],function($,swatch){
                $(document).on('swatch.initialized', function() {
                function doSomething()
                {
                // Here make some layout changes of the products list...
                }
                });
                </script>





                share|improve this answer
























                  0












                  0








                  0






                  Try With This one.



                   <script type="text/javascript">
                  require(['jquery','Magento_Swatches/js/swatch-renderer'],function($,swatch){
                  $(document).on('swatch.initialized', function() {
                  function doSomething()
                  {
                  // Here make some layout changes of the products list...
                  }
                  });
                  </script>





                  share|improve this answer












                  Try With This one.



                   <script type="text/javascript">
                  require(['jquery','Magento_Swatches/js/swatch-renderer'],function($,swatch){
                  $(document).on('swatch.initialized', function() {
                  function doSomething()
                  {
                  // Here make some layout changes of the products list...
                  }
                  });
                  </script>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered yesterday









                  Aniket PrajapatiAniket Prajapati

                  527




                  527






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Magento Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f139122%2fexecute-javascript-code-after-swatches-are-displayed-in-category-view%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      An IMO inspired problem

                      Management

                      Has there ever been an instance of an active nuclear power plant within or near a war zone?