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

200
Views
How to pass data from one event listener to another using JavaScript

I have made a calculator using JS, the number pad elements with the values from 1-9 are triggered using an event listener.

const items = document.querySelectorAll('.item-number');

These display on my screen element

let display = document.getElementById('show');

These all work fine, my issue is with the decimal point button, in which I originally tried to assign an event listener. This worked fine if i press the decimal button and then press a number button

eg; 0.2 but when i press number first then decimal i get the same as above.

I'm fairly new to JS and have tried many different ways to try and solve this, such as using concat etc

const itemDecimal = document.querySelector('.item-operator__decimal');
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

const itemNumbers = document.querySelectorAll('.item-number');
const itemDecimal = document.querySelector('.item-operator__decimal');

const itemClear = document.querySelector('.item-clear__recent');
const itemClearAll = document.querySelector('.item-clear__all');

let display = document.getElementById('show');

let outPutTextElements;

itemNumbers.forEach(item => {
    let itemTxtNumbers = [item.innerHTML]; // get string numbers 1-9 values

    item.addEventListener('click',function(){ 
        outPutTextElements= 
        document.getElementById('show').innerHTML += itemTxtNumbers; // display incremented element
        
        let number = parseFloat(outPutTextElements); // prevent zero from appending to front
        number % 1 == 0 ? number.toFixed(0): number.toFixed(1);
        document.getElementById('show').innerHTML = number; 
        
    });
});

itemDecimal.addEventListener('click',function(decimal){
    let decimalPoint = decimal.target.innerText;
    display.innerText = decimalPoint;
})

Below is the html

<div class="screen" id="show">
  <button class="item item-number">7</button>
  <button class="item item-number">8</button>
  <button class="item item-number">9</button>
  <button class="item item-clear__recent">CE</button>
  <div class="item item-clear__all">C</div>

  <button class="item item-number">4</button>
  <button class="item item-number">5</button>
  <button class="item item-number">6</button>
  <button class="item item-operator">/</button>
  <button class="item item-operator">*</button>

  <button class="item item-number">1</button>
  <button class="item item-number">2</button>
  <button class="item item-number">3</button>
  <button class="item item-operator">-</button>
  <button class="item item-operator">+</button>

  <button class="item item-number item-number__zero">0</button>
  <button class="item item-operator__decimal">.</button>
  <button class="item item-operator__equals">=</button>
</div>
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!