-1

I have a list that I am expanding and collapsing. What I want to do is only show the +- if the text in the li is over a certain length.

function prepareList() {
  $('.package_caption').find('ul').find('li')
        .click( function(event) {
        if (this == event.target) {
            $(this).toggleClass('expanded');
            $(this).children('ul').toggle('medium');
        }
        return false;
    })
    .addClass('collapsed')
        .children('ul').hide();
};
  $(document).ready( function() {
      prepareList();
});
Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47
Johanna
  • 147
  • 1
  • 10

2 Answers2

0

You can execute this in the document ready event. Simply checking the length property of the text.

var limit = 5; 
var lis=$('.package_caption').find('ul').find('li');
$.each(lis,function(ind,item){

   if($(this).text().length>limit)
   {          
       $(this).text("+");
   }
});

Here is a working sample

Shyju
  • 214,206
  • 104
  • 411
  • 497
  • I'm playing with the code now. Not working but I'm tweaking it. – Johanna Dec 16 '15 at 15:03
  • What is not working ?did yo see the sample i posted ? – Shyju Dec 16 '15 at 15:58
  • I'm a noob so much at times it hurts. I am testing, still don't have it, but wrapping the IF block around the prepareList(); in the document ready function. Can be viewed here http://test.fmanet.org/membership/test-benefits-chart/ – Johanna Dec 16 '15 at 16:26
0

I can do something like a Show / Hide link in <li>:

$(function () {
  if ($("ul").children().length > 5) {
    $("ul li:nth-child(5)").after('<li id="show-more">Show / Hide</li>');
    $("#show-more").click(function () {
      $this = $(this);
      $(this).nextAll().fadeToggle(function () {
        $this.text(function () {
          return ($this.next().is(":visible") ? "Hide" : "Show");
        });
      });
    }).html("Show").nextAll().fadeOut();
  }
});
#show-more {border: 1px solid #99f; list-style: none; cursor: pointer; padding: 3px; display: inline-block; line-height: 1; border-radius: 3px;}
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
  <li>Item 4</li>
  <li>Item 5</li>
  <li>Item 6</li>
  <li>Item 7</li>
  <li>Item 8</li>
  <li>Item 9</li>
  <li>Item 10</li>
  <li>Item 11</li>
  <li>Item 12</li>
  <li>Item 13</li>
  <li>Item 14</li>
  <li>Item 15</li>
</ul>

Output: http://output.jsbin.com/lafihexura

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • That code is beautiful. But right now I am trying to check the length of each individual (li) tag. I need the + sign from my style to show only if it is going to wrap to a second line. My test page is here http://test.fmanet.org/membership/test-benefits-chart/ – Johanna Dec 16 '15 at 16:14
  • Ah... @Johanna I have a different approach for you. Did you think about `text-overflow: ellipsis`? – Praveen Kumar Purushothaman Dec 16 '15 at 16:15
  • That is a nice approach, I am checking it out now. – Johanna Dec 16 '15 at 16:42
  • I am trying this but it cuts it off and puts a box around it. Nice but I need it to be able to wrap to two lines by clicking on a 'more' or plus sign if too long – Johanna Dec 16 '15 at 16:51