Question

Script in filename.phtml

      <script>
       require(['jquery', 'jquery/ui'], function($){

                //   $('.swatch-option')[0].css('color','red');
                  $('.swatch-option').get(0).click();
                  alert('123');

        });
</script>

Error in Console

filename.html:1028 Uncaught TypeError: Cannot read property 'click' of undefined

Was it helpful?

Solution

Add dependency 'domReady!'.

<script>
       require(['jquery', 'jquery/ui', 'domReady!'], function($){
                  $('.swatch-option').get(0).click();
                  alert('123');

        });
</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top