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

265
Views
¿Cómo agregar el atributo onClick a un botón con JS?

¿Cómo puedo agregar el atributo onClick a este botón usando JS?

<button type="submit" name="add-to-cart" value="41" class="single_add_to_cart_button button alt">BUY NOW</button>

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

0

Puedes hacerlo usando Jquery:

 "your element".addEventListener('click', function(){ alert('hey'); }, false);
about 4 years ago · Juan Pablo Isaza Report

0

puede usar la función JS .addEventListener() , aquí encontrará una explicación bien hecha de It.

Lo que le sugiero que haga es agregar una identificación a la etiqueta de su botón y luego usar document.getElementById () (si no sabe qué hace esta función JS, lea esto ) para encontrar su botón y aplicarle el .addEventListner () función para agregar un comportamiento de clic.

Hice un ejemplo de código simple a continuación, échele un vistazo y avíseme si esta respuesta es lo que está buscando.

 document.getElementById('myButton').addEventListener('click', () => { document.getElementById('myButton').classList.toggle('toggled'); });
 button { width: 100px; height: 50px; background: #ccc; border: 0; outline: 0; } button:hover { cursor: pointer } button.toggled { background: red; }
 <html> <head></head> <body> <button type=submit id="myButton"> CLICK ME </button> </body> </html>

about 4 years ago · Juan Pablo Isaza Report

0

Si desea hacerlo en js, deberá usar la función .addEventListener() o la función .onClick() que se activan cada vez que se hace clic en el botón

también puede usar .onClick dentro de su html

 <button type="submit" name="add-to-cart" value="41" class="single_add_to_cart_button button alt" onClick="function_name">BUY NOW</button>

nota al margen: si desea acceder al elemento de botón dentro de su javascript, puede agregar una identificación a su etiqueta de botón y luego usardocument.getElementById() o use let var = document.getElementByClass() pero tenga cuidado de usar var[0] para acceder al elemento del botón en caso de que lo haya filtrado por clase

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!