Skip to content

Instantly share code, notes, and snippets.

@kiub
Last active August 29, 2015 14:21
Show Gist options
  • Save kiub/0e618c8d28060f1356e7 to your computer and use it in GitHub Desktop.
Save kiub/0e618c8d28060f1356e7 to your computer and use it in GitHub Desktop.
Generate excerpt from html (trim, replace) as string
(function($) {
// jQuery function to set a maximum length or characters for a page element it can handle mutiple elements
$.fn.createExcerpts = function(elems,length,more_txt) {
$.each($(elems), function() {
var item_html = $(this).html(); //
item_html = item_html.replace(/< /?[^>]+>/gi, ''); //replace html tags
item_html = jQuery.trim(item_html); //trim whitespace
$(this).html(item_html.substring(0,length)+more_txt); //update the html on page
});
return this; //allow jQuery chaining
}
})(jQuery);
//example call
$().createExcerpts('.blogpost',280,'...');
// http://www.sitepoint.com/jquery-create-excerpts-text-elements/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment