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

169
Views
¿Debo agregar dos detectores de eventos a un solo evento?

Me gustaría dos funciones a un botón. La primera se agregará de todos modos, mientras que la segunda función depende de una condición de verdadero/falso.

El siguiente código funciona, pero ¿debería hacer algo como esto?

 <!DOCTYPE html> <html> <body> <h2>JavaScript addEventListener()</h2> <p>This example uses the addEventListener() method to attach a click event to a button.</p> <button id="myBtn">Try it</button> <p id="demo"></p> <script> document.getElementById("myBtn").addEventListener("click", displayDate); document.getElementById("myBtn").addEventListener("click", test); function displayDate() { document.getElementById("demo").innerHTML = Date(); } function test() { document.getElementById("demo").innerHTML += "test"; } </script> </body> </html>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Puedes usar condition

 const btn = document.getElementById("myBtn"); let isEvent = true; // Add 'Event' btn.addEventListener("click", displayDate); // Add conditional 'Event' if (isEvent) { btn.addEventListener("click", test); } // OR btn.addEventListener("click", e => { if (isEvent) { test(e); } });

Aviso: no olvides eliminar el evento

about 4 years ago · Juan Pablo Isaza Report

0

Para resolver su problema, haría 1 función con una función dentro que tome la decisión de su condición.

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!