JavaScript How can I trim a text like this with jquery? [duplicate]

0, I guess you misunderstood the purpose of jQuery (just read the first paragraph: "What is jQuery?"). jQuery is a framework built on top of javascript to manipulate the DOM in the browser. For what you need you only need a simple javascript expression:

function trimToLen(str, maxLen) {
    var trimmed = str.substr(0, maxLen);
    return trimmed.substr(0, trimmed.lastIndexOf(' ')) + '…';
}

to make it work for strings shorter than the maxLex you have to add an additional condition:

function trimToLen(str, maxLen) {
    if (str.length <= maxLen) {
        return str;
    }
    var trimmed = str.substr(0, maxLen);
    return trimmed.substr(0, trimmed.lastIndexOf(' ')) + '…';
}

and now the examples:

trimToLen("too short", 15);
// returns: too short
trimToLen("equal to maxLen", 15);
// returns: equal to maxLen
trimToLen("a little more than maxLen", 15);
// returns: a little more…

Notice that I used (elipsis) instead of ... (3 x dot). Visually it is is pretty much the same but you save a couple of characters and also avoid complicating the logic with the length of the trimmed text (because you have 3 instead of 1 character).

Another thing you might want is to normalise spaces in order to catch tabs, new lines or spaces. This could be the case when you want to display a longer description (containing formatted text) in one short <a> or <span> tag. For that you can use a simple regex as the first expression in the function:

str = str.replace(/\s/g, ' ');
Answer:1

i want to call a html online link using xmlhttprequest with javascript, here is my code but when the code arrive to xmlhttp.open it stopped and does not continue the execution function loadXMLDoc(...

i want to call a html online link using xmlhttprequest with javascript, here is my code but when the code arrive to xmlhttp.open it stopped and does not continue the execution function loadXMLDoc(...

  1. javascript html file to string
  2. javascript html file upload
  3. javascript html file
  4. javascript html file name
  5. javascript html file load
  6. javascript html file input
  7. javascript html file include
  8. javascript html file download
  9. javascript html file read
  10. javascript html file in variable
  11. javascript html file example
  12. javascript html file template
  13. javascript html file save
  14. javascript html file link
  15. javascript html file not found
  16. javascript html file relative path
  17. javascript html file exists
  18. javascript html file require
  19. javascript html file make
  20. javascript html file difference

guys, I am playing arround with HTML5 and javascript. The current thing which I am making is the following: there is a purple block on the screen and when you click a button it is moved 100 pixels to ...

guys, I am playing arround with HTML5 and javascript. The current thing which I am making is the following: there is a purple block on the screen and when you click a button it is moved 100 pixels to ...

  1. html5 javascript function
  2. html5 javascript function parameters
  3. html5 javascript function example
  4. html5 call javascript function
  5. html5 onclick javascript function
  6. html5 call javascript function on page load
  7. html5 href javascript function
  8. html5 button javascript function
  9. html5 execute javascript function
  10. html5 run javascript function
  11. html5 button call javascript function
  12. html5 button onclick javascript function
  13. html5 call external javascript function
  14. html5 form action javascript function
  15. html5 button onclick call javascript function
  16. html5 form action call javascript function

I am trying to create a javascript algorithm for an ul menu that stretches out li elements to use the available width (100%). I was thinking about an algorithm with this flow: 1. calculate the ...

I am trying to create a javascript algorithm for an ul menu that stretches out li elements to use the available width (100%). I was thinking about an algorithm with this flow: 1. calculate the ...

Below is the code where I am disabling ENTER button. $(document).ready(function () { $(window).keydown(function (event) { if ((event.keyCode == 13) && ($('#btnsubmit').disabled)) ...

Below is the code where I am disabling ENTER button. $(document).ready(function () { $(window).keydown(function (event) { if ((event.keyCode == 13) && ($('#btnsubmit').disabled)) ...