I have from a recommendation on my other question here:
Developed a truncate plugin, and was wondering if you guys could help me improve it or see where I have gone wrong.
Plugin code:
$.fn.vishTrunc = function(options) {
var settings = $.extend({
size: 100,
omission: '...',
}, options);
return this.each(function() {
var elements = $(this),
regex = /\s+/,
init = function() {
elements.each(function() {
var textDefault = $(this).text();
if (textDefault.length > settings.size) {
var words = textDefault.trim().split(regex).slice(0,settings.size);
var replacement = $("<p class='replacement'/>").text(words.join(" ") + settings.omission);
$(this).parent().find('.read-more').click(function(){
if($(this).text() === 'Read More'){
replacement.hide();
$(this).parent().find('p:eq(1)').show();
} else if($(this).text() === 'Read Less'){
replacement.show();
$(this).parent().find('p:eq(1)').hide();
}
$(this).text(function(i,old){
return old==='Read More' ? 'Read Less' : 'Read More';
});
return false
});
$(this).before(replacement);
$(this).hide();
}
});
};
init();
});
};