0

I have function for substring:

$('#text').on('mouseup', function(e){ 
    var text = "There are a lot of text";
    if (text != '') {
        $('.ppr .sel_text').text(text.trim().substr(0,21) + "..."); 
    }
});

How can I get only begining end ending of the text string?

For example, now result is "There are a lot of te...".

I would like to get: "There are ... text"

Ivan Ivanov
  • 515
  • 1
  • 5
  • 20

1 Answers1

1

String.prototype.substr()

Warning: Although String.prototype.substr(…) is not strictly deprecated (as in "removed from the Web standards"), it is defined in Annex B of the ECMA-262 standard, whose introduction states:


… All of the language features and behaviours specified in this annex have one or more undesirable characteristics and in the absence of legacy usage would be removed from this specification. … … Programmers should not use or assume the existence of these features and behaviours when writing new ECMAScript code. …

Use String.prototype.substring() instead. You have to take the string from both start and end. You can try the following way:

$('#text').on('mouseover', function(e){ 
  var text = "There are a lot of text";
  if (text != '') {
    $('.sel_text').text(text.substring(0,10) + "..." + text.trim().substring(text.length - 5));
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="text">There are a lot of text<div>
<div class="sel_text"></div>
Mamun
  • 66,969
  • 9
  • 47
  • 59