jQuery Click executes only once inside for loop. Tampermonkey

157 views Asked by At

Trying to change a text area to a car and then click a button to execute something and then rinse and repeat for a new car until we've done it for all cars.

var index;
var cars = ["car1" , "car2" , "car3"]
var x = document.getElementsByTagName("textarea");
for(index = 0; index < 10; index++) {
    x[0].innerHTML = cars [index];
    jQuery('#someID').click();
}

This just goes through all cars and then clicks the button once we reach the last car.

If I take out the x[0] line and just have clicked it, it does in fact click 3 times once for each item in the array, but the site I'm using pops a window up saying
please insert a car each time we click with nothing, put inside the textarea.

Why does it work when its blank, but not when I try to edit the textarea?

1

There are 1 answers

0
Jon Edwards On

I think something like this will work for you.

var cars = ["car1" , "car2" , "car3"]
for(var index = 0; index < cars.length; index++) {
  document.getElementsByTagName("textarea")[0].textContent += cars[index];
  document.getElementById("someId").click()
}