Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

207
Views
¿Cómo activar un toque de botón con javascript?

Quiero activar automáticamente un toque en un botón con javascript.

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

Intenté lo siguiente y ninguno de ellos funcionó.

 submitButton.click();

y

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

Ninguno funcionó.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Document.getElementsByName() devuelve una NodeList de elementos (no un solo elemento).

En cuanto al evento, el click debería funcionar bien:

 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 Report

0

Agregue una identificación al botón anterior con una función onClick. Cambiar el

 document.getElementsByName('name')

a

 document.getElementById('id')

Código completo:

HTML:

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

JS:

 var btn=document.getElementById("btn"); btn.click(); function clicked() { console.log("Clicked"); }

Si desea activar varios botones con la misma clase:

HTML:

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

JS:

 var btn=document.getElementsByClassName("btn"); btn[0].click(); function clicked() { console.log("Clicked"); }

Para varios botones con la misma clase, la devolución de document.getElementsByClassName devolverá una matriz de objetos. En el ejemplo anterior, he usado el primer elemento de esa matriz, pero si lo desea, puede recorrer la matriz y activar el evento de clic.

about 4 years ago · Juan Pablo Isaza Report

0

document.getElementsByName devuelve una colección NodeList de elementos con un atributo de nombre dado en el documento.

Entonces su submitButton será una matriz. Por lo tanto, debe enviar el evento de clic con 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!