Search code examples
javascriptjqueryhtmlstring-length

Smaller font-size if the string length of the div is greater than 10


I have a div that I want to detect the length of; if it's 10 or more, I want it to display in a smaller font-size.

$(window).load(function() {
  function adjustFontSize() {
    var theTitleLength = $('.article-title-on-list').text.length;
    if (theTitleLength >= 10) {
      $('.article-title-on-list').css('font-size', '1.5rem');
    } else {
      $('.article-title-on-list').css('font-size', '2rem');
    };
  };
  adjustFontSize();
};
<div class="article-title-on-list">12345678901234567890</div>

The function fails silently.


Solution

  • text is method and not property. use .text() instead of .text

     var theTitleLength = $('.article-title-on-list').text().length;