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

121
Views
Función OnClick a partir de la página de apertura

Esto puede sonar estúpido, pero escribí un código para convertir binario en un número normal. Tengo un botón que tienes que presionar que ejecuta toda la función. Sin embargo, sin hacer clic en el botón, se lanzó la función.

 let button = document.getElementById("submit"); let binary; let result = document.createElement("h1"); result.className = "answer"; button.addEventListener("click", calc()); function calc(){ binary = document.getElementById("binary").value; result.textContent = "The Result is: " + parseInt(binary, 2); document.body.appendChild(result); console.log('here'); }

Cuando cargo la página, el texto "El resultado es" ya está allí y cuando hago clic en él, no se registra en la consola ni cambia el valor. Esto puede ser estúpido, pero no puedo resolverlo, gracias de antemano.

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

0

El problema que vi en tu código es que llamaste a la función calc() en el método addEventListener, en su lugar, debes escribirlo así button.addEventListener("click", calc); , debe pasar calc y no calc() .

about 4 years ago · Juan Pablo Isaza Report

0

Utilizar este :

 let button = document.getElementById("submit"); let binary; let result = document.createElement("h1"); result.className = "answer"; button.addEventListener("click",(e)=>{ e.preventDefault(); calc(); }); function calc(){ binary = 1000101; // get binary result.textContent = "The Result is: " + parseInt(binary, 2); document.body.appendChild(result); console.log('here'); }
 <button id ="submit"> Submit </button> <br>

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!