Skip to content

Instantly share code, notes, and snippets.

@i8ramin
Created June 29, 2010 13:56
Show Gist options
  • Save i8ramin/457245 to your computer and use it in GitHub Desktop.
Save i8ramin/457245 to your computer and use it in GitHub Desktop.
/*
* truncatable 1.2 - jQuery lightwieght text truncation plugin
*
* Copyright (c) 2009 Philip Beel (http://www.theodin.co.uk/)
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*
* Revision: $Id: jquery.truncatable.js 2009-08-20 $
*
*/
(function ($) {
$.fn.truncatable = function (options) {
var defaults = {
limit: 100,
more: '...',
less: false,
hideText: '[read less]'
};
options = $.extend(defaults, options);
return this.each(function (num) {
var stringLength = $(this).html().length;
if (stringLength > defaults.limit) {
var splitText = $(this).html().substr(defaults.limit);
var splitPoint = splitText.substr(0, 1);
var whiteSpace = new RegExp(/^\s+$/);
for (var newLimit = defaults.limit; newLimit < stringLength; newLimit++) {
var newSplitText = $(this).html().substr(0, newLimit);
var newHiddenText = $(this).html().substr(newLimit);
var newSplitPoint = newSplitText.slice(-1);
if (whiteSpace.test(newSplitPoint)) {
var hiddenText = '<span class="hiddenText_' + num + '" style="display:none">' + newHiddenText + '</span>';
var setNewLimit = (newLimit - 1);
var trunkLink = $('<a>').attr('class', 'more_' + num + '');
$(this).html($(this).html().substr(0, setNewLimit)).append('<a class="more_' + num + '" href="#">' + defaults.more + '<a/> ' + hiddenText);
$('a.more_' + num).bind('click', function () {
$('span.hiddenText_' + num).show();
$('a.more_' + num).hide();
if (defaults.less === true) {
$('span.hiddenText_' + num).append('<a class="hide_' + num + '" href="" title="' + defaults.hideText + '">' + defaults.hideText + '</a>');
$('a.hide_' + num).bind('click', function () {
$('.hiddenText_' + num).hide();
$('.more_' + num).show();
$('.hide_' + num).empty();
return false;
});
}
return false;
});
newLimit = stringLength;
}
}
}
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment