Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit inner text of div to 175 characters?

Tags:

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 exaple on how to do this for a input text box or text area, but not a div. Can anyone help with this?

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>
like image 696
TruMan1 Avatar asked Aug 08 '11 14:08

TruMan1


People also ask

How do I restrict text length in HTML?

The HTML <input> tag is used to get user input in HTML. To give a limit to the input field, use the min and max attributes, which is to specify a maximum and minimum value for an input field respectively. To limit the number of characters, use the maxlength attribute.

How do I limit characters in text CSS?

While you can't use CSS alone to do this, you can limit the amount of characters show using CSS as Darren has suggested. You need to set your text container to white-space: no-wrap, text-overflow: ellipsis, and overflow:hidden. Then simply set the size for your container.


1 Answers

You can use the the form of text() that takes a function and write something like:

$("div.some-area").text(function(index, currentText) {
    return currentText.substr(0, 175);
});
like image 108
Frédéric Hamidi Avatar answered Oct 03 '22 11:10

Frédéric Hamidi