Last active
August 29, 2015 14:21
-
-
Save kiub/0e618c8d28060f1356e7 to your computer and use it in GitHub Desktop.
Generate excerpt from html (trim, replace) as string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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