Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

152
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda