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

201
Visualizações
How to output a specific element value to console.log by clicking a specific element

enter image description here

As shown in the picture above, when the first element value is 7, If I click, I want to output 7 in console.log.

var container1 = document.createElement('div')
container1.className = 'container1';
document.body.appendChild(container1);
for(var index = 0;index < 20 ; index++){
   var aa = document.createElement('span')
   aa.innerHTML = card1[index];
   container1.appendChild(aa);
   aa.className = 'card'+index;
   aa.addEventListener('click',clickEvent)
}
function clickEvent(){
   //What code should I use?
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

how are you?

Try this:

function clickEvent(evt){
   console.log(evt.target.innerHTML)
}
about 4 years ago · Juan Pablo Isaza Relatório

0

If you have provided the HTML and CSS too then it will be a direct answer to your need but a similar approach can be done using this keyword which print innerHTML of each span tag on click

function paret(reed) {
  console.log(reed.innerHTML)
}
div {
  display: flex;
  justify-content: space-between;
}

span {
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 80px;
  height: 160px;
  background-color: red
}
<div>
  <span onclick="paret(this)">7</span>
  <span onclick="paret(this)">17</span>
  <span onclick="paret(this)">9</span>
  <span onclick="paret(this)">8</span>
  <span onclick="paret(this)">5</span>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

For this you could use the textContent property of HTML elements in JavaScript. To complete this you would also need to pass the event into your clickEvent function. Click events have a event.target, which is the HTML node that was clicked. The event

var container1 = document.createElement('div')
container1.className = 'container1';
document.body.appendChild(container1);
for(var index = 0;index < 20 ; index++){
   var aa = document.createElement('span')
   aa.innerHTML = card1[index];
   container1.appendChild(aa);
   aa.className = 'card'+index;
   aa.addEventListener('click',clickEvent)
}
function clickEvent(event){
   //What code should I use?
   let clickedBox = event.target;
   console.log(clickedBox.textContent);
}

As far as I know, this should work like you're intending. I haven't worked with the front-end side of JS lately, so correct me if I'm wrong.

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