Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

208
Vistas
How to trigger a button tap with javascript?

I want to automatically trigger a tap on a button with javascript.

var submitButton = document.getElementsByName('name');

I tried the following and none of them worked.

submitButton.click();

and

const touchEvent = new TouchEvent("touchstart", {
    touches: [touch],
    view: window,
    cancelable: true,
    bubbles: true,
});
submitButton.dispatchEvent(touchEvent);

Neither worked.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Document.getElementsByName() returns a NodeList of elements (not a single element).

As far as the event, click should work just fine:

for (const elm of document.getElementsByTagName('button')) {
  elm.addEventListener('click', (ev) => console.log(ev.target.textContent));
}

const buttons = document.getElementsByName('name');

for (const button of buttons) {
  button.click();
}
<div>
  <button name="name">one</button>
  <button name="name">two</button>
  <button>three</button>
</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Add an id to the above button with a onClick function. Change the

document.getElementsByName('name')

to

document.getElementById('id')

Full Code:

Html:

<button id="btn" onClick="clicked()">
Click me
</button>

JS:

var btn=document.getElementById("btn");
btn.click();

function clicked() {
    console.log("Clicked");
}

If you want to trigger multiple buttons with same class:

Html:

<button class="btn" onClick="clicked()">
Click me
</button>

JS:

var btn=document.getElementsByClassName("btn");
btn[0].click();

function clicked() {
    console.log("Clicked");
}

For multiple button with same class, the return of the document.getElementsByClassName will return an array of object. In the above example, I have used the first element of that array, but if you want, you can loop through the array and trigger the click event.

about 4 years ago · Juan Pablo Isaza Denunciar

0

document.getElementsByName returns a NodeList Collection of elements with a given name attribute in the document.

So your submitButton will be an array. So you have to dispatch the click event with submitButton[0].click()

var submitButton = document.getElementsByName('name');
submitButton[0].click();
function submitClick() {
  console.log('submitClick')
}
<button name="name" onclick="submitClick()">Submit</button>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda