4

There is some text comprised of 165 characters. I need to show only the first 155 characters, but the last 5 out of the 155 chars should be replaced with "....." (elipses), and the remaining characters should be removed. fiddle

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="jquery-1.8.3.js"></script>
<script type="text/javascript">
    $(function(){
        var txt= $('div').text();
        for(i=0;i<2;i++){
            alert(txt.charAt(150+ i))
        }
    })
</script>
</head>
<body>
<div style="width:100px">
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
</div>
</body>
2
  • 3
    Come on @TecHunter, he has a fiddle with things he trying. You have to start learning from somewhere. A -1 on a question with an included fiddle that shows he is trying to learn should get you a -1 yourself. Did you know string functions from day 1? Commented May 30, 2013 at 15:54
  • @JClaspill okay okay... you are right about question format Commented May 30, 2013 at 15:55

5 Answers 5

14

Check out the substring function here, and voilà:

var txt= $('#restrict').text();
if(txt.length > 155)
    $('#result').text(txt.substring(0,150) + '.....');

http://jsfiddle.net/techunter/GRmY2/

Sign up to request clarification or add additional context in comments.

2 Comments

You could remove the rude comment and this would get an upvote.
okay sorry, you are right, I've seen worse questions that's true. I even upvoted your comment as self punishement for being too harsh for this late afternoon :P
5

Depending on what browsers you're targeting, you could just use css.

text-overflow: ellipsis

1 Comment

nice but too specific imo :)
2

Looks like TecHunter already posted pretty much the same thing, but I was bored and did this.

At 155 chars replaces last 5 characters with "....". Has a textarea input with a character counter that updates as you type.

Html

<div>
    <h2>Input</h2>
    <textarea id="sampleInput">
    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
    </textarea>
</div>
<div>
    <label>Character Count:</label> <span id ="charCounter"></span>
<div>
<h2>Output</h2>
<p style="width:100px" id="sampleOutput"></p>

JS

updateOutput();

$('#sampleInput').keyup(function(e){
   updateOutput();
});

function updateOutput(){
    var sampleInput = $('#sampleInput').val(),
        sampleInputLength = sampleInput.length;

    if(sampleInputLength >= 155) {    
        sampleInput = sampleInput.substr(0,150) + ".....";    
    }

    $('#charCounter').text(sampleInputLength);
    $('#sampleOutput').text(sampleInput);
}

CSS

#sampleInput {
    width: 400px;
    height:100px;    
}

jsfiddle

2 Comments

I +1 for the effort :) and because you are bored
@TecHunter If I was really bored, I'd turn this into a plugin.
1

I saw some nice answers, however, some of them are not efficient in other cases, ex: when you copy and paste something. So, I would suggest:

 $(document).on('keyup keypress blur change', '#elementId', function () {
     var maxLength = 100; // number of characters to limit
     imposeMaxLength($(this), maxLength);
 });

 function imposeMaxLength(element, maxLength) {
     if (element.val().length > maxLength) {
         element.val(element.val().substring(0, maxLength));
     }
 }

Using imposeMaxLength as function, you can reuse it in other events.

Comments

0
$(".btn-custom").text(function(index, currentText) {
    if(currentText.length > 60){
        return currentText.substr(0, 20)+" ...";
    }
});

1 Comment

Please add little briefly about the code you written

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.