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

150
Visualizações
javascript button on clock

Now I have issue that only first clicked button works when pressing others post buttons after first clicked button, it just disappears and not loading comments,

Adding snipped to clarify issue. It's first time using this feature. Sorry for issues, may you can help me with that.

I'm using django as backend.

var currentItems = 0;
            
function loadcomments(d) {
  var post = d.getAttribute("data-post");
  const elementList = document.querySelectorAll('#comment'+post);
  for (let i = currentItems; i < currentItems + 2; i++) {
    if (elementList[i]) {
      elementList[i].style.display = 'block';
    }  
  }
  currentItems += 2;
  if (currentItems >= elementList.length) {
    event.target.style.display = 'none';  
  }
}
.comment {
  display: none;
}
<div class='post'>
<div class='comment' id="comment1">
  11
</div>

<div class='comment' id="comment1">
  12
</div>

<div class='comment' id="comment1">
  13
</div>

<div class='comment' id="comment1">
  14
</div>

<div class='comment' id="comment1">
  15
</div>

<a class="loadmore" href="javascript:void(0)" 
onclick="loadcomments(this)" data-post="1">Load more</a>
</div>

<div class='post'>
<div class='comment' id="comment2">
  21
</div>

<div class='comment' id="comment2">
  22
</div>

<div class='comment' id="comment2">
  23
</div>



<a class="loadmore" href="javascript:void(0)" 
onclick="loadcomments(this)" data-post="2">Load more</a>
  
</div>

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

0

As Teemu mentioned, "You've to declare and initialize currentItems outside of the function".

var currentItems = 0;
            
function loadcomments(d) {
  var post = d.getAttribute("data-post");
  const elementList = document.querySelectorAll('#comment'+post);
  for (let i = currentItems; i < currentItems + 2; i++) {
    if (elementList[i]) {
      elementList[i].style.display = 'block';
    }  
  }
  currentItems += 2;
  if (currentItems >= elementList.length) {
    event.target.style.display = 'none';  
  }
}
div {
  display: none;
}
<div id="comment1">
  1
</div>

<div id="comment1">
  2
</div>

<div id="comment1">
  3
</div>

<div id="comment1">
  4
</div>

<div id="comment1">
  5
</div>

<a class="loadmore" href="javascript:void(0)" onclick="loadcomments(this)" data-post="1">Load more</a>

about 4 years ago · Juan Pablo Isaza Relatório

0

You need to set special currentItems variable for each posts.

remove the global currentItems variable and Try

function loadcomments(d) {
    var post = d.getAttribute("data-post");
    var currentItems = d.getAttribute("data-currentItems") | 0;
    const elementList = document.querySelectorAll('#comment'+post);
    for (let i = currentItems; i < currentItems + 2; i++) {
        if (elementList[i]) {
            elementList[i].style.display = 'block';
        }  
    }
    currentItems += 2;
    d.setAttribute("data-currentItems", currentItems);
    if (currentItems >= elementList.length) {
        event.target.style.display = 'none';  
    }
}
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