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

199
Views
The "like button" function only increases the number of likes for a certain section whenever it's clicked

There's something wrong with my heart buttons under the "Recommended for You" section. On the "Recipe of the Day" post, the heart button works just fine. What I'm trying to do is get the number of likes for the posts on the right side to increase too. However, whenever I click the other heart buttons under the "Recommended for You" section, they only increase the number of likes for the ROTD post.

Here's the JavaScript and HTML code I used:

console.log("JavaScript is connected");

let numCount = 68
let count = document.querySelector('.count')
function addLike () {
    numCount ++
    count.innerText = numCount
}
<div class="likebtn1">
  <button class= "heartbtn" onclick="addLike()"><img src="imgs/heart.png" class="heart" alt="the heart-like button"></button>
  <p class="yurilike"><span class="count">68</span></p>
</div>

<div class="likebtn2">
  <button class="heartbtn" onclick="addLike()"><img src="imgs/heart.png" class="heart2" alt="the heart-like button"></button> 
  <p class="Emilike"><span class="count">212</span></p>
</div>

<div class="likebtn3">
  <button class="heartbtn" onclick="addLike()"><img src="imgs/heart.png" class="heart3" alt="the heart-like button"></button> 
  <p class="Marisalike"><span class="count">33</span></p>
</div>

Whenever I try to create separate functions for the RFY section posts and do the same thing for the other heart buttons, they all just stop working and don't do anything.

Here's a video to show what I'm talking about: https://www.youtube.com/watch?v=8NYqizDBkZk

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

0

It's because querySelector always returns the first element that matches the query. When you do

document.querySelector('.count')

It's always giving you the same .count div, regardless of which like button was pressed.

You need to use a more specific query like .likebtn2 .count, or give each count a unique class name.

about 4 years ago · Juan Pablo Isaza Report

0

document.body.addEventListener('click', (event) => {
  const button = event.target.closest('.button');

  if (!button) return;

  const container = button.closest('.likes');

  if (!container) return;

  const counter = container.querySelector('.counter');

  if (!counter) return;

  counter.innerText = Number(counter.innerText) + 1;
});
<div class="likes">
  <button class="button">I love it!</button>
  <div class="counter">0</div>
</div>

<div class="likes">
  <button class="button">I love it!</button>
  <div class="counter">0</div>
</div>

<div class="likes">
  <button class="button">I love it!</button>
  <div class="counter">0</div>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

Change your JS code to the one I have and then add the this keyword to your button that invokes the JS function.

function addLike(e){
  let count = Number(e.nextElementSibling.innerText) + 1;
  e.nextElementSibling.innerText = count;
}
<div class="likebtn1">
  <button class= "heartbtn" onclick="addLike(this)"><img src="imgs/heart.png" class="heart" alt="the heart-like button"></button>
  <p class="yurilike"><span class="count">68</span></p>
</div>

<div class="likebtn2">
  <button class="heartbtn" onclick="addLike(this)"><img src="imgs/heart.png" class="heart2" alt="the heart-like button"></button> 
  <p class="Emilike"><span class="count">212</span></p>
</div>

<div class="likebtn3">
  <button class="heartbtn" onclick="addLike(this)"><img src="imgs/heart.png" class="heart3" alt="the heart-like button"></button> 
  <p class="Marisalike"><span class="count">33</span></p>
</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!