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

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

Add dependency 'domReady!'.

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

        });
</script>
  • Cannot read property 'click' of undefined same error showing in console – Shafeel Sha Jul 10 at 10:38
  • you write in file static ? – Evgeniy Kapelko Jul 10 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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