Hide and reveal text using Javascript

Asked

Viewed 693 times

2

I have a div that has news, I need to hide a part of the text when the file is read by the browser, in addition the user can see the full text if he wants to click on "See More" which is a tag <a> and then reduce it. I came to this:

<script type="text/javascript">
//quando a pagina for carregada o texto da notícia será reduzido a 100 letras apenas.
$(document).ready(function(){
    txt = document.getElementById('texto-principal-noticia').innerHTML;
    var txt2 = txt.substr(0,100);
    var complemento = '...';
    txt3 = txt2 + complemento;
    document.getElementById('texto-principal-noticia').innerHTML = txt3;
});
    function Mostrar(){
       if(txt<txt3){
           document.getElementById('texto-principal-noticia').innerHTML = txt;
           document.getElementById('link-noticia').innerHTML = 'Mostrar Menos'; 
       }else{
           document.getElementById('texto-principal-noticia').innerHTML = txt3;
           document.getElementById('link-noticia').innerHTML = 'Mostrar Mais'; 
       }
}

With this you can hide the text and open it when the function mostrar is called, but when the function is called again to this time hide the text it does not work, someone knows tell me why?

I think the problem is in the probation check, but when I change the operators it still doesn’t work.

  • It would be better to use Ellipsis in overflow, and just change the div’s style, wouldn’t it? You can do this with pure CSS, no JS, following functional demonstration here: https://answall.com/a/110958/70

3 answers

2

In the if, you should buy the size of the text (in characters) and not as if they were the whole type. Something like this:

if(txt.length < txt3.length) //aqui, eu verifico a quantidade de caracteres no texto.
  • if(txt<txt2 ){ also works, see the run in my reply

1

You don’t need this

var complemento = '...';
txt3 = txt2 + complemento;

In addition, the answer of dvd is absolutely correct, look

$(document).ready(function(){
   txt = document.getElementById('texto-principal-noticia').innerHTML;
   txt2 = txt.substr(0,100)+'...';
   document.getElementById('texto-principal-noticia').innerHTML = txt2;

  if(txt<txt2 ){
    document.getElementById('texto-principal-noticia').innerHTML = txt;
    document.getElementById('link-noticia').innerHTML = 'Mostrar Menos'; 
  }else{
     document.getElementById('texto-principal-noticia').innerHTML = txt2;
     document.getElementById('link-noticia').innerHTML = 'Mostrar Mais'; 
  }
});
    
function Mostrar(){

   if(document.getElementById('link-noticia').innerHTML == 'Mostrar Mais'){
     document.getElementById('texto-principal-noticia').innerHTML = txt;
     document.getElementById('link-noticia').innerHTML = 'Mostrar Menos'; 
   }else{
     document.getElementById('texto-principal-noticia').innerHTML = txt2;
     document.getElementById('link-noticia').innerHTML = 'Mostrar Mais'; 
   }

}
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    <p id="texto-principal-noticia">
    O que é Lorem Ipsum?
    Lorem Ipsum é simplesmente uma simulação de texto da indústria tipográfica e de impressos, e vem sendo utilizado desde o século XVI, quando um impressor desconhecido pegou uma bandeja de tipos e os embaralhou para fazer um livro de modelos de tipos. Lorem Ipsum sobreviveu não só a cinco séculos, como também ao salto para a editoração eletrônica, permanecendo essencialmente inalterado. Se popularizou na década de 60, quando a Letraset lançou decalques contendo passagens de Lorem Ipsum, e mais recentemente quando passou a ser integrado a softwares de editoração eletrônica como Aldus PageMaker.
    </p>
    <button id="link-noticia" onclick="Mostrar()">Mostrar Mais</button>

0

You can directly compare the innerHTML in the element #link-noticia:

function Mostrar(){
   if(document.getElementById('link-noticia').innerHTML == 'Mostrar Mais'){
     document.getElementById('texto-principal-noticia').innerHTML = txt;
     document.getElementById('link-noticia').innerHTML = 'Mostrar Menos'; 
   }else{
     document.getElementById('texto-principal-noticia').innerHTML = txt3;
     document.getElementById('link-noticia').innerHTML = 'Mostrar Mais'; 
   }
}
  • That if I with ternary conditional operator as would be? : )

  • document.getElementById('link-noticia').innerHTML == 'Mostrar Mais' ?&#xA; (document.getElementById('texto-principal-noticia').innerHTML = txt,&#xA; document.getElementById('link-noticia').innerHTML = 'Mostrar Menos' )&#xA; :&#xA; (document.getElementById('texto-principal-noticia').innerHTML = txt,&#xA; document.getElementById('link-noticia').innerHTML = 'Mostrar Mais')&#xA; ;

  • Boy I was putting ponto e virgula ao invés de comma. Mas nesse caso para maior legibilidade o if Else` gets much better

  • Yes. The ternaries in these cases are not worth it. Also because the guy may later want to include something else in the if and it gets hard to read, like you said.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.