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

0

how are you?

Try this:

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

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 Report

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