'How can I the limit inner text of a div to 175 characters?
I have a div area that has an arbitrary number of characters. I want to limit the amount of characters that show up in there to 175 characters. I mostly found examples on how to do this for an input text box or text area, but not a div. How can I do it?
This is my code I am trying to limit to 175 characters:
<div class="some-area">
Blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
</div>
Solution 1:[1]
Replace div with the correct identifier for your content:
$("div").text($(this).text().substr(0, 175)+'...');
Remove the ellipses from the end if desired. I would recommend leaving them to let your users know that the text was shortened. If you are just trying to limit the text to 175 without any notice, then you may choose to remove them in that case as well.
Solution 2:[2]
I just did this for one of my projects. I found this great plugin.
$('some-area').expander({
slicePoint:175
});
Solution 3:[3]
Instead of just cutting it off at 175 characters, add an ellipsis to the div if the text runs over the maximum width: https://web.archive.org/web/20110828173959/http://plugins.jquery.com/plugin-tags/ellipsis
Solution 4:[4]
var textis="";textis=$(".some-area").html().substring(0, 175);$(".some-area").html(textis);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Robert |
| Solution 2 | Lightness Races in Orbit |
| Solution 3 | josliber |
| Solution 4 | Nouman Saleem |
