How to make sure jQueryUI button text does not exceed 25 characters?

To avoid problems with my layout, how can I make sure that the text of my button does not exceed 25 characters?

var fileName = "AnalysisOfCollateralDebt";

$('#buttonContainer')
.append("<button id='fileButton' class='span-1'>Show File" 
+ fileName 
+ "</button>");

$("#fileButton").button({
    icons: {primary: "ui-icon-triangle-1-e"},
    text: true
});

In the above example, the button will read:

Show File: AnalysisOfCollateralDebt

but I want him to read:

Show File: AnalysisOf...
+3
source share
1 answer

Just change line 5 to this:

+ fileName.substr (0,10) + ((fileName.length> 10)? '...': '')

in English:

fileName.substr(0,10): first 10 characters of the file name; if the file name is shorter than 10 characters .substr()will have no effect.

((fileName.length > 10)?'...':''): if the file name is really longer than 10 characters, print '...'.

+1
source

Source: https://habr.com/ru/post/1777185/


All Articles