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

176
Visualizações
Read More Function for unordered list using jQuery with Paragraph

I'm a beginner, and trying to understand everything. I tried using <p> and it works smoothly but when I insert a text like a ul and li it wont show up

I also tried using a div but only the <a> part is working not the ul and li. I hope someone can help me.

$(document).ready(function() {
  $(".more-less").click(function() {
    $(this).parent().prev('ul.more').toggleClass("main");
    if ($(this).parent().prev('ul.more').hasClass('main')) {
      $(this).text('Read Less');
    } else {
      $(this).text('Read More');
    }
  });
});
.more {
  text-align: left !important;
  display: none;
}

.more-less {
  /* position: absolute; */
  /* right: -30px; */
  /* top: -34px; */
  color: #e43330 !important;
  text-align: center;
  float: left;
  width: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<p style="text-align: left;">First paragraph then cuts...
  <p class="more">Second paragraph.</p>
  <p><strong>Header key roles</strong></p>
  <ul class="more">
    <li>trait 1</li>
    <li>trait 2</li>
    <li>trait 3</li>
    <li>trait 4</li>
    <li>trait 5</li>
  </ul>
  <a class="more-less">Read More</a> &nbsp;
  <p> end paragraph</p>

strong text

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

0

There's two main issues in your code. Firstly the ul.more is not a parent element of the clicked p, so parents().next() isn't the correct traversal logic to use. As the target element is a sibling of the one which raised the event, use siblings().

Secondly, you need to set display: block on the hidden ul. You can do this through CSS, on the .main class which you toggle.

Also note that you cannot nest p elements. I corrected this in the example below by converting the containing element to a div.

jQuery($ => {
  $(".more-less").click(function() {
    let $more = $(this).siblings('ul.more').toggleClass("main");
    
    if ($more.hasClass('main')) {
      $(this).text('Read Less');
    } else {
      $(this).text('Read More');
    }
  });
});
.more {
  text-align: left !important;
  display: none;
}

.more.main {
  display: block;
}

.more-less {
  color: #e43330 !important;
  text-align: center;
  float: left;
  width: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div>
  First paragraph then cuts...
  <p class="more">Second paragraph.</p>
  <p><strong>Header key roles</strong></p>
  <ul class="more">
    <li>trait 1</li>
    <li>trait 2</li>
    <li>trait 3</li>
    <li>trait 4</li>
    <li>trait 5</li>
  </ul>
  <a class="more-less">Read More</a> &nbsp;
  <p> end paragraph</p>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Try delete display: none; from more class

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