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

125
Views
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 answers
Answer question

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 Report

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