Search code examples
javascripthtmlgoogle-chrome-extension

JavaScript script click button


I have a chrome extension and i need JavaScript to click on this button, but it is not working.

This is the button that needs to be clicked with JavaScript.

<a class="button checkout" data-no-turbolink="true" href="...">checkout</a>

This is what I tried but it is not working

document.getElementsByClassName("button checkout").click();


Solution

  • You need to specify the index

    document.getElementsByClassName("button checkout")[0].click()
    

    function clickme(){
      document.getElementsByClassName("button checkout")[0].click()
    }
    
    function clicked(){
      alert('It works!!!!!!')
    }
    <a class="button checkout" data-no-turbolink="true" href="www.google.com" onclick="clicked()">checkout</a>
    <hr/>
    <button onclick="clickme()">Click div</button>