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

151
Visualizações
Want to toggle 2 different list on click of two different button with same java script code

Issues
There are following issues which are as follows :

  • First list is working fine but it has problem that bullets disappears on clcik of button - I know it can be solved by using .Card1.short>li:nth-child(n + 6){display: block;} only but I need to hide lists tag by default
  • Second issue is that second button is not working on its own list but changing first one - I need to used same single JS as there can be more than 10 lists . Also the innerHTML of both buttons works fine as changes individually on click

Code

function reed(saad) {
  var ul = document.getElementById("Card1");
  button = document.getElementById("demo1");
  ul.classList.toggle("short");
  if (saad.innerHTML == "Show More") {
    saad.innerHTML = "Show Less";
  } else {
    saad.innerHTML = "Show More";
  }
}
.Card1.short > li:nth-child(n + 6) {
  display: block;
}
.Card1 > li:nth-child(n + 6) {
  display: none;
}
ul{float:left;}
<ul class="Card1" id="Card1">
  <li>Windows 10</li>
  <li>8GB Ram</li>
  <li>1TB SSD</li>
  <li>Intel Core i7 (11th Gen)</li>
  <li>NVIDIA GeForce</li>
  <li>Full HD Display</li>
  <li>Dolby Audio</li>
  <li>1 Warranty</li>
  <button id="demo1" onclick="reed(this)">Show More</button>
</ul>


<ul class="Card2" id="Card2">
  <li>Windows 20</li>
  <li>16GB Ram</li>
  <li>10TB SSD</li>
  <li>Intel Core i15 (21th Gen)</li>
  <li>NVIDIA</li>
  <li>Double HD Display</li>
  <li>Dolby Audio</li>
  <li>2 Warranty</li>
  <button id="demo2" onclick="reed(this)">Show More</button>
</ul>

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

0

I've edited your code. Instead of selecting an element by id, use this to get parentElement and toggle the class short.

Also if you want the list marked with bullet, use display:list-item. The list-item keyword causes the element to generate a ::marker pseudo-element box.

function reed(saad) {
  saad.parentElement.classList.toggle("short");
  if (saad.innerHTML == "Show More") {
    saad.innerHTML = "Show Less";
  } else {
    saad.innerHTML = "Show More";
  }
}
.Card1.short > li:nth-child(n + 6), .Card2.short > li:nth-child(n + 6) {
  display: list-item;
}

.Card1 > li:nth-child(n + 6), .Card2 > li:nth-child(n + 6) {
  display: none;
}

ul{
  float:left;
}
<ul class="Card1" id="Card1">
  <li>Windows 10</li>
  <li>8GB Ram</li>
  <li>1TB SSD</li>
  <li>Intel Core i7 (11th Gen)</li>
  <li>NVIDIA GeForce</li>
  <li>Full HD Display</li>
  <li>Dolby Audio</li>
  <li>1 Warranty</li>
  <button id="demo1" onclick="reed(this)">Show More</button>
</ul>


<ul class="Card2" id="Card2">
  <li>Windows 20</li>
  <li>16GB Ram</li>
  <li>10TB SSD</li>
  <li>Intel Core i15 (21th Gen)</li>
  <li>NVIDIA</li>
  <li>Double HD Display</li>
  <li>Dolby Audio</li>
  <li>2 Warranty</li>
  <button id="demo2" onclick="reed(this)">Show More</button>
</ul>

about 4 years ago · Juan Pablo Isaza Relatório

0

You can try the following, which points to the parent element of the button, rather than using a fixed id. Also in the css, "list-item" rather than "block" will keep the bullets.

function reed(saad) {
    saad.parentElement.classList.toggle("short");
    if (saad.innerHTML == "Show More") {
        saad.innerHTML = "Show Less";
    } else {
        saad.innerHTML = "Show More";
    }
}
<ul class="Card" id="Card1">
    <li>Windows 10</li>
    <li>8GB Ram</li>
    <li>1TB SSD</li>
    <li>Intel Core i7 (11th Gen)</li>
    <li>NVIDIA GeForce</li>
    <li>Full HD Display</li>
    <li>Dolby Audio</li>
    <li>1 Warranty</li>
    <button id="demo1" onclick="reed(this)">Show More</button>
</ul>


<ul class="Card" id="Card2">
    <li>Windows 20</li>
    <li>16GB Ram</li>
    <li>10TB SSD</li>
    <li>Intel Core i15 (21th Gen)</li>
    <li>NVIDIA</li>
    <li>Double HD Display</li>
    <li>Dolby Audio</li>
    <li>2 Warranty</li>
    <button id="demo2" onclick="reed(this)">Show More</button>
</ul>
.Card.short > li:nth-child(n + 6) {
    display: list-item;
}
.Card > li:nth-child(n + 6) {
    display: none;
}
ul{float:left;}

Check this fiddle if you want to generate the cards using javascript: https://jsfiddle.net/anixiapetha/o7fyjcnL/2/

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