views:

28

answers:

1

Hello all

As the title says, I want to truncate user-input text string based on the width and height of a designated container. My specification is to truncate the string, display some message like Read More at the end and when user clicks on it, the text slides down.

UPDATE: Ah! Forgot one thing. It should handle multi-byte characters as well.

Can somebody throw some light on what options do I have? jQuery plugins or some nifty jquery snippet?

Thanks and Regards

+1  A: 

jQuery Expander Plugin:

HTML:

<div class="expandable">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
    incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud 
    exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor.
    </p>
    <p>Reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. 
    Excepteur sint occaecat cupidatat non proident, sunt in culpa qui 
    officia deserunt mollit anim id est laborum.
    </p>
    </div>

Javascript:

<script src="jquery.js" type="text/javascript"></script>
<script src="jquery.expander.js" type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function() {

  // simple example, using all default options
  $('div.expandable p').expander();

  // *** OR ***

  // override some default options
  $('div.expandable p').expander({
    slicePoint:       80,  // default is 100
    expandText:         '[...]', // default is 'read more...'
    collapseTimer:    5000, // re-collapses after 5 seconds; default is 0, so no re-collapsing
    userCollapseText: '[^]'  // default is '[collapse expanded text]'
  });

});
</script>

Source:

http://plugins.learningjquery.com/expander/index.html

See demo here:

http://plugins.learningjquery.com/expander/demo/index.html

Good luck ask me if you need clarifications!

Trufa
@Trufa - +1 Thanks.
ShiVik
No problem! if you need any help implementing do ask!
Trufa
@Trufa - There's one question about this. I am displaying my expandable text string in a list. So there are multiple rows of expandable text. If I attach the `expander()` method to all of those rows, will this expand all the strings upon clicking the `expandText`?
ShiVik
No it shouldn´t look at Demo: "Example 1" But you would actually have to try it out because I can´t guarantee that it wont! Please tell me if you get that problem!
Trufa
@Trufa - Worked out well. Thanks. Didn't give any problems with multi-byte characters or the scenario I stated above.
ShiVik
Great glad to hear!
Trufa