Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

178
Visualizações
need to print a number when clicked one by one

I am making a calculator with pure javascript. I need the number inside the button to print out if I click the button once. like if I click 1 it should display 1 on my screen and if I clicked 2 it should display 2 on my screen. only the number 2 without 1...

<button>1</button>
<button>2</button>
<button>3</button>
<button>4</button>
<button>5</button>
<button>6</button>
<button>6</button>
<button>8</button>
<button>9</button>
<button>10</button>

how can I do that without the onclick event?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

const display = document.getElementById('display')
document.getElementById('buttons').addEventListener('click', ({target}) => {
  console.log(target.innerText)
  display.innerHTML = target.innerText;
})
<div id="buttons">
  <button>1</button>
  <button>2</button>
  <button>3</button>
  <button>4</button>
  <button>5</button>
  <button>6</button>
  <button>6</button>
  <button>8</button>
  <button>9</button>
  <button>10</button>
</div>
<div id="display"></div>

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use document.querySlectorAll. This will give nodelist, then iterate through it and add event listener , so that on click it displays the text content to a display area

const displayContainer = document.getElementById('display')
document.querySelectorAll('button').forEach((item) => {
  item.addEventListener('click', (e) => {
    displayContainer.innerHTML = e.target.textContent.trim();
  })
})
<button>1</button>
<button>2</button>
<button>3</button>
<button>4</button>
<button>5</button>
<button>6</button>
<button>6</button>
<button>8</button>
<button>9</button>
<button>10</button>
<div id='display'></div>

about 4 years ago · Juan Pablo Isaza Relatório

0

This should get you going.

  • Use [querySelectorAll] to select all the buttons.
  • Add eventListener to all the buttons.
  • Use event.target to get the value of button and make the logic from there.

const h1 = document.querySelector("h1");
const btns = document.querySelectorAll("button");
btns.forEach(btn => {
    btn.addEventListener('click', event => {
    h1.textContent = event.target.textContent;
  });
});
<button>1</button>
<button>2</button>
<button>3</button>
<button>4</button>
<button>5</button>
<button>6</button>
<button>6</button>
<button>8</button>
<button>9</button>
<button>0</button>

<h1></h1>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda