0

I'm trying to count the number of lines in a string for each div. But I would also like to set a maximum number and if the string inside a div is equal/greater to that number to show a button.

Here's a codepen

jQuery:

$('.intro_text').each(function() {
   var lines = $(this).find("div").val().split("\n");  
   var maxLine = 5;
   if(lines > maxLine){
      $(this).after("<button>toggle</button>");
   }
 });

1 Answers1

1

You are not counting the length of array returned when you split the text of the divs. Change your code to this.

$('.intro_text').each(function() {
  var lines = $(this).find("div").text().split("\n").length;  
  var maxLine = 5;

  if(lines >= maxLine){
    $(this).after("<button>toggle</button>");
  }
});
Qonvex620
  • 3,819
  • 1
  • 8
  • 15