How to put ellipses in a text with JQUERY

1

I have a table which brings me from the database a description which varies the number of characters, the problem is that if a description has "1000" characters are shown in the table and that looks bad then I look for the way of being able to shorten those texts so that it does not look bad visually ...
PS: I have seen that with css it can be done but I am not interested in doing it that way since I am using datatables ....
thanks

    
asked by JDavid 25.01.2017 в 15:18
source

2 answers

2

function cortarTextoConPuntos(texto, limite)
{
  var puntosSuspensivos = "...";
  if(texto.length > limite)
  {
    texto = texto.substring(0,limite) + puntosSuspensivos;
  }

    return texto;
}

console.log(cortarTextoConPuntos("hola amigos",9));
    
answered by 25.01.2017 / 15:58
source
3

I leave you a generic function .. You call it with the id of the div you want to format, along with the number of characters.

function ellipsisJS ( containerId , largomaximo) {
var $container = $("#" + containerId); 
var $text = $("#descripcion p");    

while ( $container.text().length > largomaximo ) {
    $text.text(function (index, text) {
        return text.replace(/\W*\s(\S)*$/, '...');
    });
}
}

And you invoke it like that

ellipsisJS("descripcion", 100);  

An example of how it works .. link

The credits for link

    
answered by 25.01.2017 в 15:55