How to toggle Truncate text in JavaScript?











up vote
3
down vote

favorite
1












Example here:
enter image description here



How to toggle text from 'truncate' to 'full text'?



like i want to toggle full text when a person clicks on 'read more' button and also hide text when 'hide text' button is clicked



Snippet:






var textHolder = document.querySelector('.demo');
var btn = document.querySelector('.btn');


function Truancate(textHolder, limit) {
let txt = textHolder.innerHTML;
if (txt.length > limit) {
let newText = txt.substr(0, limit) + ' ...';
textHolder.innerHTML = newText;
}
}

Truancate(textHolder, textHolder.offsetWidth / 10);



function toggleText() {
// here i want to show full text...
// and also -> btn.innerHTML = 'Hide Text' | 'Show Text;
}


btn.addEventListener('click', toggleText);

<section class="demo" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>

<button class="readMore btn">Read More</button>












share|improve this question




























    up vote
    3
    down vote

    favorite
    1












    Example here:
    enter image description here



    How to toggle text from 'truncate' to 'full text'?



    like i want to toggle full text when a person clicks on 'read more' button and also hide text when 'hide text' button is clicked



    Snippet:






    var textHolder = document.querySelector('.demo');
    var btn = document.querySelector('.btn');


    function Truancate(textHolder, limit) {
    let txt = textHolder.innerHTML;
    if (txt.length > limit) {
    let newText = txt.substr(0, limit) + ' ...';
    textHolder.innerHTML = newText;
    }
    }

    Truancate(textHolder, textHolder.offsetWidth / 10);



    function toggleText() {
    // here i want to show full text...
    // and also -> btn.innerHTML = 'Hide Text' | 'Show Text;
    }


    btn.addEventListener('click', toggleText);

    <section class="demo" id="demo">
    Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
    line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
    </section>

    <button class="readMore btn">Read More</button>












    share|improve this question


























      up vote
      3
      down vote

      favorite
      1









      up vote
      3
      down vote

      favorite
      1






      1





      Example here:
      enter image description here



      How to toggle text from 'truncate' to 'full text'?



      like i want to toggle full text when a person clicks on 'read more' button and also hide text when 'hide text' button is clicked



      Snippet:






      var textHolder = document.querySelector('.demo');
      var btn = document.querySelector('.btn');


      function Truancate(textHolder, limit) {
      let txt = textHolder.innerHTML;
      if (txt.length > limit) {
      let newText = txt.substr(0, limit) + ' ...';
      textHolder.innerHTML = newText;
      }
      }

      Truancate(textHolder, textHolder.offsetWidth / 10);



      function toggleText() {
      // here i want to show full text...
      // and also -> btn.innerHTML = 'Hide Text' | 'Show Text;
      }


      btn.addEventListener('click', toggleText);

      <section class="demo" id="demo">
      Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
      line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
      </section>

      <button class="readMore btn">Read More</button>












      share|improve this question















      Example here:
      enter image description here



      How to toggle text from 'truncate' to 'full text'?



      like i want to toggle full text when a person clicks on 'read more' button and also hide text when 'hide text' button is clicked



      Snippet:






      var textHolder = document.querySelector('.demo');
      var btn = document.querySelector('.btn');


      function Truancate(textHolder, limit) {
      let txt = textHolder.innerHTML;
      if (txt.length > limit) {
      let newText = txt.substr(0, limit) + ' ...';
      textHolder.innerHTML = newText;
      }
      }

      Truancate(textHolder, textHolder.offsetWidth / 10);



      function toggleText() {
      // here i want to show full text...
      // and also -> btn.innerHTML = 'Hide Text' | 'Show Text;
      }


      btn.addEventListener('click', toggleText);

      <section class="demo" id="demo">
      Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
      line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
      </section>

      <button class="readMore btn">Read More</button>








      var textHolder = document.querySelector('.demo');
      var btn = document.querySelector('.btn');


      function Truancate(textHolder, limit) {
      let txt = textHolder.innerHTML;
      if (txt.length > limit) {
      let newText = txt.substr(0, limit) + ' ...';
      textHolder.innerHTML = newText;
      }
      }

      Truancate(textHolder, textHolder.offsetWidth / 10);



      function toggleText() {
      // here i want to show full text...
      // and also -> btn.innerHTML = 'Hide Text' | 'Show Text;
      }


      btn.addEventListener('click', toggleText);

      <section class="demo" id="demo">
      Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
      line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
      </section>

      <button class="readMore btn">Read More</button>





      var textHolder = document.querySelector('.demo');
      var btn = document.querySelector('.btn');


      function Truancate(textHolder, limit) {
      let txt = textHolder.innerHTML;
      if (txt.length > limit) {
      let newText = txt.substr(0, limit) + ' ...';
      textHolder.innerHTML = newText;
      }
      }

      Truancate(textHolder, textHolder.offsetWidth / 10);



      function toggleText() {
      // here i want to show full text...
      // and also -> btn.innerHTML = 'Hide Text' | 'Show Text;
      }


      btn.addEventListener('click', toggleText);

      <section class="demo" id="demo">
      Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
      line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
      </section>

      <button class="readMore btn">Read More</button>






      javascript html5 css3






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 5:07









      S.Serpooshan

      3,93821637




      3,93821637










      asked Nov 20 at 4:54









      TheV

      387




      387
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          3
          down vote













          You don't need javascript for this. a simple css text-overflow: ellipsis; can do the trick.



          It is a better/standard way to truncate the long text because it will truncate the text display at exact position which is depend on the font-size, width of parent container, etc... that is not possible simply with js. here is a demo:






          var textHolder = document.querySelector('.demo');
          var btn = document.querySelector('.btn');

          function toggleText() {
          textHolder.classList.toggle("truncate");
          }

          btn.addEventListener('click', toggleText);
          toggleText(); //to truncate at first time

          .truncate {
          text-overflow: ellipsis;
          /*BOTH of the following are required for text-overflow (overflow: hidden; and white-space: nowrap;)*/
          overflow: hidden;
          white-space: nowrap;
          }

          <section class="demo" id="demo">
          Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
          Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
          Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
          </section>

          <button class="readMore btn">Read More</button>





          Note that cutting innerHTML can be dangerous as you may cut at improper position and corrupt the HTML code opening/closing tags...






          share|improve this answer



















          • 2




            a better answer indeed :)
            – Towkir Ahmed
            Nov 20 at 5:14


















          up vote
          1
          down vote













          First you need to store the full text to a variable, never change that variable, then store the truncated text into another variable and finally toggle between those to variable value to set the text to your target element.



          Here is the sinppet:






          var textHolder = document.querySelector('.demo');
          var fullText = textHolder.innerHTML;
          var btn = document.querySelector('.btn');
          var textStatus = 'full'; // use this to check the status of text and toggle;

          function Truancate(textHolder, limit) {
          let txt = textHolder.innerHTML;
          if (txt.length > limit) {
          let newText = txt.substr(0, limit) + ' ...';
          textHolder.innerHTML = newText;
          textStatus = 'truncated';
          }
          }

          Truancate(textHolder, textHolder.offsetWidth / 10);

          function toggleText() {
          // here i want to show full text...
          // and also -> btn.innerHTML = 'Hide Text' | 'Show Text;
          if (textStatus === 'truncated') {
          textHolder.innerHTML = fullText;
          textStatus = 'full';
          } else {
          Truancate(textHolder, textHolder.offsetWidth / 10);
          }
          }


          btn.addEventListener('click', toggleText);

          <section class="demo" id="demo">
          Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
          line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
          </section>

          <button class="readMore btn">Read More</button>








          share|improve this answer




























            up vote
            0
            down vote













            Here is some more object oriented way and can be used anywhere in like situation



            What you need to do is only follow following step




            1. Create a wrapper container with an id let' say readMoreContainer


            2. Add a class named readMoreText to the section containing the text to toggle truncate


            3. Add a class named readMoreButton to the button element.



            You can remove your unnecessary classes and ids






            var ReadMore = (function() {
            function ReadMore(limit, readMoreContainerElementSelector) {
            this.limit = limit;
            this.readMoreContainerElementSelector = readMoreContainerElementSelector;
            this.isFullTextShown = false;
            this.initializeReadMore();
            }
            ReadMore.prototype.initializeReadMore = function() {
            this.fullText = document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML;
            this.truncatedText = this.fullText.substr(0, this.limit) + ' ...';
            var that = this;
            document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").addEventListener("click", function() {
            that.performToogle();
            });
            this.performToogle();
            };
            ReadMore.prototype.performToogle = function() {
            var textToDisplay = "";
            var buttonText = "";
            this.isFullTextShown ? (textToDisplay = this.fullText, buttonText = "Read Less" ): (textToDisplay = this.truncatedText, buttonText = "Read More");
            this.isFullTextShown = !this.isFullTextShown;
            document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML = textToDisplay;
            document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").innerHTML = buttonText;
            };
            return ReadMore;
            }());

            var readmore = new ReadMore(100, "#readMoreContainer1");
            var readmore = new ReadMore(50, "#readMoreContainer2");

            <div id="readMoreContainer1">
            <section class="demo readMoreText" id="demo">
            Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
            line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
            </section>
            <button class="readMore btn readMoreButton">Read More</button>
            </div>
            <hr/>
            <div id="readMoreContainer2">
            <section class="demo readMoreText" id="demo">
            Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
            line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
            </section>
            <button class="readMore btn readMoreButton">Read More</button>
            </div>








            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386455%2fhow-to-toggle-truncate-text-in-javascript%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              3
              down vote













              You don't need javascript for this. a simple css text-overflow: ellipsis; can do the trick.



              It is a better/standard way to truncate the long text because it will truncate the text display at exact position which is depend on the font-size, width of parent container, etc... that is not possible simply with js. here is a demo:






              var textHolder = document.querySelector('.demo');
              var btn = document.querySelector('.btn');

              function toggleText() {
              textHolder.classList.toggle("truncate");
              }

              btn.addEventListener('click', toggleText);
              toggleText(); //to truncate at first time

              .truncate {
              text-overflow: ellipsis;
              /*BOTH of the following are required for text-overflow (overflow: hidden; and white-space: nowrap;)*/
              overflow: hidden;
              white-space: nowrap;
              }

              <section class="demo" id="demo">
              Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
              Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
              Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
              </section>

              <button class="readMore btn">Read More</button>





              Note that cutting innerHTML can be dangerous as you may cut at improper position and corrupt the HTML code opening/closing tags...






              share|improve this answer



















              • 2




                a better answer indeed :)
                – Towkir Ahmed
                Nov 20 at 5:14















              up vote
              3
              down vote













              You don't need javascript for this. a simple css text-overflow: ellipsis; can do the trick.



              It is a better/standard way to truncate the long text because it will truncate the text display at exact position which is depend on the font-size, width of parent container, etc... that is not possible simply with js. here is a demo:






              var textHolder = document.querySelector('.demo');
              var btn = document.querySelector('.btn');

              function toggleText() {
              textHolder.classList.toggle("truncate");
              }

              btn.addEventListener('click', toggleText);
              toggleText(); //to truncate at first time

              .truncate {
              text-overflow: ellipsis;
              /*BOTH of the following are required for text-overflow (overflow: hidden; and white-space: nowrap;)*/
              overflow: hidden;
              white-space: nowrap;
              }

              <section class="demo" id="demo">
              Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
              Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
              Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
              </section>

              <button class="readMore btn">Read More</button>





              Note that cutting innerHTML can be dangerous as you may cut at improper position and corrupt the HTML code opening/closing tags...






              share|improve this answer



















              • 2




                a better answer indeed :)
                – Towkir Ahmed
                Nov 20 at 5:14













              up vote
              3
              down vote










              up vote
              3
              down vote









              You don't need javascript for this. a simple css text-overflow: ellipsis; can do the trick.



              It is a better/standard way to truncate the long text because it will truncate the text display at exact position which is depend on the font-size, width of parent container, etc... that is not possible simply with js. here is a demo:






              var textHolder = document.querySelector('.demo');
              var btn = document.querySelector('.btn');

              function toggleText() {
              textHolder.classList.toggle("truncate");
              }

              btn.addEventListener('click', toggleText);
              toggleText(); //to truncate at first time

              .truncate {
              text-overflow: ellipsis;
              /*BOTH of the following are required for text-overflow (overflow: hidden; and white-space: nowrap;)*/
              overflow: hidden;
              white-space: nowrap;
              }

              <section class="demo" id="demo">
              Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
              Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
              Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
              </section>

              <button class="readMore btn">Read More</button>





              Note that cutting innerHTML can be dangerous as you may cut at improper position and corrupt the HTML code opening/closing tags...






              share|improve this answer














              You don't need javascript for this. a simple css text-overflow: ellipsis; can do the trick.



              It is a better/standard way to truncate the long text because it will truncate the text display at exact position which is depend on the font-size, width of parent container, etc... that is not possible simply with js. here is a demo:






              var textHolder = document.querySelector('.demo');
              var btn = document.querySelector('.btn');

              function toggleText() {
              textHolder.classList.toggle("truncate");
              }

              btn.addEventListener('click', toggleText);
              toggleText(); //to truncate at first time

              .truncate {
              text-overflow: ellipsis;
              /*BOTH of the following are required for text-overflow (overflow: hidden; and white-space: nowrap;)*/
              overflow: hidden;
              white-space: nowrap;
              }

              <section class="demo" id="demo">
              Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
              Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
              Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
              </section>

              <button class="readMore btn">Read More</button>





              Note that cutting innerHTML can be dangerous as you may cut at improper position and corrupt the HTML code opening/closing tags...






              var textHolder = document.querySelector('.demo');
              var btn = document.querySelector('.btn');

              function toggleText() {
              textHolder.classList.toggle("truncate");
              }

              btn.addEventListener('click', toggleText);
              toggleText(); //to truncate at first time

              .truncate {
              text-overflow: ellipsis;
              /*BOTH of the following are required for text-overflow (overflow: hidden; and white-space: nowrap;)*/
              overflow: hidden;
              white-space: nowrap;
              }

              <section class="demo" id="demo">
              Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
              Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
              Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
              </section>

              <button class="readMore btn">Read More</button>





              var textHolder = document.querySelector('.demo');
              var btn = document.querySelector('.btn');

              function toggleText() {
              textHolder.classList.toggle("truncate");
              }

              btn.addEventListener('click', toggleText);
              toggleText(); //to truncate at first time

              .truncate {
              text-overflow: ellipsis;
              /*BOTH of the following are required for text-overflow (overflow: hidden; and white-space: nowrap;)*/
              overflow: hidden;
              white-space: nowrap;
              }

              <section class="demo" id="demo">
              Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
              Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
              Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
              </section>

              <button class="readMore btn">Read More</button>






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 20 at 5:23

























              answered Nov 20 at 5:06









              S.Serpooshan

              3,93821637




              3,93821637








              • 2




                a better answer indeed :)
                – Towkir Ahmed
                Nov 20 at 5:14














              • 2




                a better answer indeed :)
                – Towkir Ahmed
                Nov 20 at 5:14








              2




              2




              a better answer indeed :)
              – Towkir Ahmed
              Nov 20 at 5:14




              a better answer indeed :)
              – Towkir Ahmed
              Nov 20 at 5:14












              up vote
              1
              down vote













              First you need to store the full text to a variable, never change that variable, then store the truncated text into another variable and finally toggle between those to variable value to set the text to your target element.



              Here is the sinppet:






              var textHolder = document.querySelector('.demo');
              var fullText = textHolder.innerHTML;
              var btn = document.querySelector('.btn');
              var textStatus = 'full'; // use this to check the status of text and toggle;

              function Truancate(textHolder, limit) {
              let txt = textHolder.innerHTML;
              if (txt.length > limit) {
              let newText = txt.substr(0, limit) + ' ...';
              textHolder.innerHTML = newText;
              textStatus = 'truncated';
              }
              }

              Truancate(textHolder, textHolder.offsetWidth / 10);

              function toggleText() {
              // here i want to show full text...
              // and also -> btn.innerHTML = 'Hide Text' | 'Show Text;
              if (textStatus === 'truncated') {
              textHolder.innerHTML = fullText;
              textStatus = 'full';
              } else {
              Truancate(textHolder, textHolder.offsetWidth / 10);
              }
              }


              btn.addEventListener('click', toggleText);

              <section class="demo" id="demo">
              Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
              line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
              </section>

              <button class="readMore btn">Read More</button>








              share|improve this answer

























                up vote
                1
                down vote













                First you need to store the full text to a variable, never change that variable, then store the truncated text into another variable and finally toggle between those to variable value to set the text to your target element.



                Here is the sinppet:






                var textHolder = document.querySelector('.demo');
                var fullText = textHolder.innerHTML;
                var btn = document.querySelector('.btn');
                var textStatus = 'full'; // use this to check the status of text and toggle;

                function Truancate(textHolder, limit) {
                let txt = textHolder.innerHTML;
                if (txt.length > limit) {
                let newText = txt.substr(0, limit) + ' ...';
                textHolder.innerHTML = newText;
                textStatus = 'truncated';
                }
                }

                Truancate(textHolder, textHolder.offsetWidth / 10);

                function toggleText() {
                // here i want to show full text...
                // and also -> btn.innerHTML = 'Hide Text' | 'Show Text;
                if (textStatus === 'truncated') {
                textHolder.innerHTML = fullText;
                textStatus = 'full';
                } else {
                Truancate(textHolder, textHolder.offsetWidth / 10);
                }
                }


                btn.addEventListener('click', toggleText);

                <section class="demo" id="demo">
                Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
                line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
                </section>

                <button class="readMore btn">Read More</button>








                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  First you need to store the full text to a variable, never change that variable, then store the truncated text into another variable and finally toggle between those to variable value to set the text to your target element.



                  Here is the sinppet:






                  var textHolder = document.querySelector('.demo');
                  var fullText = textHolder.innerHTML;
                  var btn = document.querySelector('.btn');
                  var textStatus = 'full'; // use this to check the status of text and toggle;

                  function Truancate(textHolder, limit) {
                  let txt = textHolder.innerHTML;
                  if (txt.length > limit) {
                  let newText = txt.substr(0, limit) + ' ...';
                  textHolder.innerHTML = newText;
                  textStatus = 'truncated';
                  }
                  }

                  Truancate(textHolder, textHolder.offsetWidth / 10);

                  function toggleText() {
                  // here i want to show full text...
                  // and also -> btn.innerHTML = 'Hide Text' | 'Show Text;
                  if (textStatus === 'truncated') {
                  textHolder.innerHTML = fullText;
                  textStatus = 'full';
                  } else {
                  Truancate(textHolder, textHolder.offsetWidth / 10);
                  }
                  }


                  btn.addEventListener('click', toggleText);

                  <section class="demo" id="demo">
                  Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
                  line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
                  </section>

                  <button class="readMore btn">Read More</button>








                  share|improve this answer












                  First you need to store the full text to a variable, never change that variable, then store the truncated text into another variable and finally toggle between those to variable value to set the text to your target element.



                  Here is the sinppet:






                  var textHolder = document.querySelector('.demo');
                  var fullText = textHolder.innerHTML;
                  var btn = document.querySelector('.btn');
                  var textStatus = 'full'; // use this to check the status of text and toggle;

                  function Truancate(textHolder, limit) {
                  let txt = textHolder.innerHTML;
                  if (txt.length > limit) {
                  let newText = txt.substr(0, limit) + ' ...';
                  textHolder.innerHTML = newText;
                  textStatus = 'truncated';
                  }
                  }

                  Truancate(textHolder, textHolder.offsetWidth / 10);

                  function toggleText() {
                  // here i want to show full text...
                  // and also -> btn.innerHTML = 'Hide Text' | 'Show Text;
                  if (textStatus === 'truncated') {
                  textHolder.innerHTML = fullText;
                  textStatus = 'full';
                  } else {
                  Truancate(textHolder, textHolder.offsetWidth / 10);
                  }
                  }


                  btn.addEventListener('click', toggleText);

                  <section class="demo" id="demo">
                  Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
                  line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
                  </section>

                  <button class="readMore btn">Read More</button>








                  var textHolder = document.querySelector('.demo');
                  var fullText = textHolder.innerHTML;
                  var btn = document.querySelector('.btn');
                  var textStatus = 'full'; // use this to check the status of text and toggle;

                  function Truancate(textHolder, limit) {
                  let txt = textHolder.innerHTML;
                  if (txt.length > limit) {
                  let newText = txt.substr(0, limit) + ' ...';
                  textHolder.innerHTML = newText;
                  textStatus = 'truncated';
                  }
                  }

                  Truancate(textHolder, textHolder.offsetWidth / 10);

                  function toggleText() {
                  // here i want to show full text...
                  // and also -> btn.innerHTML = 'Hide Text' | 'Show Text;
                  if (textStatus === 'truncated') {
                  textHolder.innerHTML = fullText;
                  textStatus = 'full';
                  } else {
                  Truancate(textHolder, textHolder.offsetWidth / 10);
                  }
                  }


                  btn.addEventListener('click', toggleText);

                  <section class="demo" id="demo">
                  Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
                  line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
                  </section>

                  <button class="readMore btn">Read More</button>





                  var textHolder = document.querySelector('.demo');
                  var fullText = textHolder.innerHTML;
                  var btn = document.querySelector('.btn');
                  var textStatus = 'full'; // use this to check the status of text and toggle;

                  function Truancate(textHolder, limit) {
                  let txt = textHolder.innerHTML;
                  if (txt.length > limit) {
                  let newText = txt.substr(0, limit) + ' ...';
                  textHolder.innerHTML = newText;
                  textStatus = 'truncated';
                  }
                  }

                  Truancate(textHolder, textHolder.offsetWidth / 10);

                  function toggleText() {
                  // here i want to show full text...
                  // and also -> btn.innerHTML = 'Hide Text' | 'Show Text;
                  if (textStatus === 'truncated') {
                  textHolder.innerHTML = fullText;
                  textStatus = 'full';
                  } else {
                  Truancate(textHolder, textHolder.offsetWidth / 10);
                  }
                  }


                  btn.addEventListener('click', toggleText);

                  <section class="demo" id="demo">
                  Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
                  line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
                  </section>

                  <button class="readMore btn">Read More</button>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 at 5:08









                  Towkir Ahmed

                  920619




                  920619






















                      up vote
                      0
                      down vote













                      Here is some more object oriented way and can be used anywhere in like situation



                      What you need to do is only follow following step




                      1. Create a wrapper container with an id let' say readMoreContainer


                      2. Add a class named readMoreText to the section containing the text to toggle truncate


                      3. Add a class named readMoreButton to the button element.



                      You can remove your unnecessary classes and ids






                      var ReadMore = (function() {
                      function ReadMore(limit, readMoreContainerElementSelector) {
                      this.limit = limit;
                      this.readMoreContainerElementSelector = readMoreContainerElementSelector;
                      this.isFullTextShown = false;
                      this.initializeReadMore();
                      }
                      ReadMore.prototype.initializeReadMore = function() {
                      this.fullText = document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML;
                      this.truncatedText = this.fullText.substr(0, this.limit) + ' ...';
                      var that = this;
                      document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").addEventListener("click", function() {
                      that.performToogle();
                      });
                      this.performToogle();
                      };
                      ReadMore.prototype.performToogle = function() {
                      var textToDisplay = "";
                      var buttonText = "";
                      this.isFullTextShown ? (textToDisplay = this.fullText, buttonText = "Read Less" ): (textToDisplay = this.truncatedText, buttonText = "Read More");
                      this.isFullTextShown = !this.isFullTextShown;
                      document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML = textToDisplay;
                      document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").innerHTML = buttonText;
                      };
                      return ReadMore;
                      }());

                      var readmore = new ReadMore(100, "#readMoreContainer1");
                      var readmore = new ReadMore(50, "#readMoreContainer2");

                      <div id="readMoreContainer1">
                      <section class="demo readMoreText" id="demo">
                      Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
                      line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
                      </section>
                      <button class="readMore btn readMoreButton">Read More</button>
                      </div>
                      <hr/>
                      <div id="readMoreContainer2">
                      <section class="demo readMoreText" id="demo">
                      Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
                      line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
                      </section>
                      <button class="readMore btn readMoreButton">Read More</button>
                      </div>








                      share|improve this answer

























                        up vote
                        0
                        down vote













                        Here is some more object oriented way and can be used anywhere in like situation



                        What you need to do is only follow following step




                        1. Create a wrapper container with an id let' say readMoreContainer


                        2. Add a class named readMoreText to the section containing the text to toggle truncate


                        3. Add a class named readMoreButton to the button element.



                        You can remove your unnecessary classes and ids






                        var ReadMore = (function() {
                        function ReadMore(limit, readMoreContainerElementSelector) {
                        this.limit = limit;
                        this.readMoreContainerElementSelector = readMoreContainerElementSelector;
                        this.isFullTextShown = false;
                        this.initializeReadMore();
                        }
                        ReadMore.prototype.initializeReadMore = function() {
                        this.fullText = document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML;
                        this.truncatedText = this.fullText.substr(0, this.limit) + ' ...';
                        var that = this;
                        document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").addEventListener("click", function() {
                        that.performToogle();
                        });
                        this.performToogle();
                        };
                        ReadMore.prototype.performToogle = function() {
                        var textToDisplay = "";
                        var buttonText = "";
                        this.isFullTextShown ? (textToDisplay = this.fullText, buttonText = "Read Less" ): (textToDisplay = this.truncatedText, buttonText = "Read More");
                        this.isFullTextShown = !this.isFullTextShown;
                        document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML = textToDisplay;
                        document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").innerHTML = buttonText;
                        };
                        return ReadMore;
                        }());

                        var readmore = new ReadMore(100, "#readMoreContainer1");
                        var readmore = new ReadMore(50, "#readMoreContainer2");

                        <div id="readMoreContainer1">
                        <section class="demo readMoreText" id="demo">
                        Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
                        line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
                        </section>
                        <button class="readMore btn readMoreButton">Read More</button>
                        </div>
                        <hr/>
                        <div id="readMoreContainer2">
                        <section class="demo readMoreText" id="demo">
                        Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
                        line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
                        </section>
                        <button class="readMore btn readMoreButton">Read More</button>
                        </div>








                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          Here is some more object oriented way and can be used anywhere in like situation



                          What you need to do is only follow following step




                          1. Create a wrapper container with an id let' say readMoreContainer


                          2. Add a class named readMoreText to the section containing the text to toggle truncate


                          3. Add a class named readMoreButton to the button element.



                          You can remove your unnecessary classes and ids






                          var ReadMore = (function() {
                          function ReadMore(limit, readMoreContainerElementSelector) {
                          this.limit = limit;
                          this.readMoreContainerElementSelector = readMoreContainerElementSelector;
                          this.isFullTextShown = false;
                          this.initializeReadMore();
                          }
                          ReadMore.prototype.initializeReadMore = function() {
                          this.fullText = document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML;
                          this.truncatedText = this.fullText.substr(0, this.limit) + ' ...';
                          var that = this;
                          document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").addEventListener("click", function() {
                          that.performToogle();
                          });
                          this.performToogle();
                          };
                          ReadMore.prototype.performToogle = function() {
                          var textToDisplay = "";
                          var buttonText = "";
                          this.isFullTextShown ? (textToDisplay = this.fullText, buttonText = "Read Less" ): (textToDisplay = this.truncatedText, buttonText = "Read More");
                          this.isFullTextShown = !this.isFullTextShown;
                          document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML = textToDisplay;
                          document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").innerHTML = buttonText;
                          };
                          return ReadMore;
                          }());

                          var readmore = new ReadMore(100, "#readMoreContainer1");
                          var readmore = new ReadMore(50, "#readMoreContainer2");

                          <div id="readMoreContainer1">
                          <section class="demo readMoreText" id="demo">
                          Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
                          line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
                          </section>
                          <button class="readMore btn readMoreButton">Read More</button>
                          </div>
                          <hr/>
                          <div id="readMoreContainer2">
                          <section class="demo readMoreText" id="demo">
                          Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
                          line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
                          </section>
                          <button class="readMore btn readMoreButton">Read More</button>
                          </div>








                          share|improve this answer












                          Here is some more object oriented way and can be used anywhere in like situation



                          What you need to do is only follow following step




                          1. Create a wrapper container with an id let' say readMoreContainer


                          2. Add a class named readMoreText to the section containing the text to toggle truncate


                          3. Add a class named readMoreButton to the button element.



                          You can remove your unnecessary classes and ids






                          var ReadMore = (function() {
                          function ReadMore(limit, readMoreContainerElementSelector) {
                          this.limit = limit;
                          this.readMoreContainerElementSelector = readMoreContainerElementSelector;
                          this.isFullTextShown = false;
                          this.initializeReadMore();
                          }
                          ReadMore.prototype.initializeReadMore = function() {
                          this.fullText = document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML;
                          this.truncatedText = this.fullText.substr(0, this.limit) + ' ...';
                          var that = this;
                          document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").addEventListener("click", function() {
                          that.performToogle();
                          });
                          this.performToogle();
                          };
                          ReadMore.prototype.performToogle = function() {
                          var textToDisplay = "";
                          var buttonText = "";
                          this.isFullTextShown ? (textToDisplay = this.fullText, buttonText = "Read Less" ): (textToDisplay = this.truncatedText, buttonText = "Read More");
                          this.isFullTextShown = !this.isFullTextShown;
                          document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML = textToDisplay;
                          document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").innerHTML = buttonText;
                          };
                          return ReadMore;
                          }());

                          var readmore = new ReadMore(100, "#readMoreContainer1");
                          var readmore = new ReadMore(50, "#readMoreContainer2");

                          <div id="readMoreContainer1">
                          <section class="demo readMoreText" id="demo">
                          Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
                          line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
                          </section>
                          <button class="readMore btn readMoreButton">Read More</button>
                          </div>
                          <hr/>
                          <div id="readMoreContainer2">
                          <section class="demo readMoreText" id="demo">
                          Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
                          line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
                          </section>
                          <button class="readMore btn readMoreButton">Read More</button>
                          </div>








                          var ReadMore = (function() {
                          function ReadMore(limit, readMoreContainerElementSelector) {
                          this.limit = limit;
                          this.readMoreContainerElementSelector = readMoreContainerElementSelector;
                          this.isFullTextShown = false;
                          this.initializeReadMore();
                          }
                          ReadMore.prototype.initializeReadMore = function() {
                          this.fullText = document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML;
                          this.truncatedText = this.fullText.substr(0, this.limit) + ' ...';
                          var that = this;
                          document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").addEventListener("click", function() {
                          that.performToogle();
                          });
                          this.performToogle();
                          };
                          ReadMore.prototype.performToogle = function() {
                          var textToDisplay = "";
                          var buttonText = "";
                          this.isFullTextShown ? (textToDisplay = this.fullText, buttonText = "Read Less" ): (textToDisplay = this.truncatedText, buttonText = "Read More");
                          this.isFullTextShown = !this.isFullTextShown;
                          document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML = textToDisplay;
                          document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").innerHTML = buttonText;
                          };
                          return ReadMore;
                          }());

                          var readmore = new ReadMore(100, "#readMoreContainer1");
                          var readmore = new ReadMore(50, "#readMoreContainer2");

                          <div id="readMoreContainer1">
                          <section class="demo readMoreText" id="demo">
                          Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
                          line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
                          </section>
                          <button class="readMore btn readMoreButton">Read More</button>
                          </div>
                          <hr/>
                          <div id="readMoreContainer2">
                          <section class="demo readMoreText" id="demo">
                          Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
                          line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
                          </section>
                          <button class="readMore btn readMoreButton">Read More</button>
                          </div>





                          var ReadMore = (function() {
                          function ReadMore(limit, readMoreContainerElementSelector) {
                          this.limit = limit;
                          this.readMoreContainerElementSelector = readMoreContainerElementSelector;
                          this.isFullTextShown = false;
                          this.initializeReadMore();
                          }
                          ReadMore.prototype.initializeReadMore = function() {
                          this.fullText = document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML;
                          this.truncatedText = this.fullText.substr(0, this.limit) + ' ...';
                          var that = this;
                          document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").addEventListener("click", function() {
                          that.performToogle();
                          });
                          this.performToogle();
                          };
                          ReadMore.prototype.performToogle = function() {
                          var textToDisplay = "";
                          var buttonText = "";
                          this.isFullTextShown ? (textToDisplay = this.fullText, buttonText = "Read Less" ): (textToDisplay = this.truncatedText, buttonText = "Read More");
                          this.isFullTextShown = !this.isFullTextShown;
                          document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML = textToDisplay;
                          document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").innerHTML = buttonText;
                          };
                          return ReadMore;
                          }());

                          var readmore = new ReadMore(100, "#readMoreContainer1");
                          var readmore = new ReadMore(50, "#readMoreContainer2");

                          <div id="readMoreContainer1">
                          <section class="demo readMoreText" id="demo">
                          Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
                          line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
                          </section>
                          <button class="readMore btn readMoreButton">Read More</button>
                          </div>
                          <hr/>
                          <div id="readMoreContainer2">
                          <section class="demo readMoreText" id="demo">
                          Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
                          line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
                          </section>
                          <button class="readMore btn readMoreButton">Read More</button>
                          </div>






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 20 at 5:54









                          Samuel J Mathew

                          3,40412228




                          3,40412228






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386455%2fhow-to-toggle-truncate-text-in-javascript%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Tonle Sap (See)

                              I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

                              Guatemaltekische Davis-Cup-Mannschaft