jQuery addClass example

Change class name on click in jQuery

HTML

<div class="container">
  It is
</div>

<div class="container">
  It is a long 
</div>

<div class="container">
  It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using
</div>

CSS

.length-15 {
  color: red;
}

.length-30 {
  color: green;
}

.length-45 {
  color: orange;
}

JavaScript

$(".container").each(function(i) {
  if ($(this).text().length <= 30) {
    $(this).addClass("length-15");
  }
});