Get the longest text in HTML document using Jquery or javascript

Here is one way you could attempt to do it:

var longest = "";

$('p').each(function() {

  if ($(this).text().length > longest.length) {

    longest = $(this).text();

  }

});

console.log(longest);

Here is a JSFiddle showing how it works: https://jsfiddle.net/2yne0qno/

Leave a Comment