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

122
Vistas
OnClick function starting at opening page

This may sound stupid but i wrote some code to convert binary into a normal number. I have a button that you have to press which runs the entire function. However, without clicking the button the function launched.

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');
}

When i load the page the 'The result is' text is already there and when i do click it it wont console log or change the value. This may be stupid but i cant figure it out thanks in advance.

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

0

The issue I saw from your code is that you called the function calc() in the addEventListener method, instead, you should write it like this button.addEventListener("click", calc);, you should pass calc and not calc().

about 4 years ago · Juan Pablo Isaza Denunciar

0

Use This :

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 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