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

249
Views
How to get value of clicked button in calculator

I have multiple buttons containing different values

<button data-action="digit" class="button" id="1">1</button>
<button data-action="digit" class="button" id="2">2</button>
<button data-action="digit" class="button" id="3">3</button> 

And I want to get these buttons to display on my calculator using this in javascript:

function digit_pressed(digit) {
    

    console.log("digit pressed: " + digit);
}

But I am unsure what to add to my function. Any help would be greatly appreciated :)

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

0

You can use querySelectorAll for select all button then use addEventListener for add click event and last step use textContent for number of digit and call function.

document.querySelectorAll('button').forEach(el =>{
  el.addEventListener('click', () =>{
    digit_pressed(el.textContent);
  });
});
function digit_pressed(digit) {
  console.log("digit pressed: " + digit);
}
<button data-action="digit" class="button" id="1">1</button>
<button data-action="digit" class="button" id="2">2</button>
<button data-action="digit" class="button" id="3">3</button>

Reference:

  • querySelectorAll
  • addEventListener
  • textContent
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!