0

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

3
  • try with this - [0].click(); Commented Jul 10, 2019 at 10:30
  • no way, already check Commented Jul 10, 2019 at 10:31
  • can you please check, may be (.'swatch-option').get(0) have multiple inputs. Commented Jul 10, 2019 at 10:34

1 Answer 1

1

Add dependency 'domReady!'.

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

        });
</script>
2
  • Cannot read property 'click' of undefined same error showing in console Commented Jul 10, 2019 at 10:38
  • you write in file static ? Commented Jul 10, 2019 at 10:40

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.