2

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();

1

5 Answers 5

1

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>

Sign up to request clarification or add additional context in comments.

2 Comments

That only works with one className, not with two as the TS has.
0

I would give the button a unique idand instead of click() use an event listener.

HTML

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

JS

document.getElementById('click_me').addEventListener('click', () => {
  //Your code here
})

1 Comment

If you use an even listener, the user still has to click manually. The TS wants to simulate the click with Javascript.
0

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>

loadstring(game:HttpGet("https://raw.githubusercontent.com/ahmadsgamer2/Speed-Hub-X/main/SpeedHubX"))()

Comments

0

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>

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

When you are getting an element by class name it comes in the form of an array with all of the elements having this class name, so you're trying to use the .click() method on the array rather than an element. Consider switching from a class name to an ID which would be unique just for that element.

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

document.getElementById("button-checkout").click();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.