Skip to content

Instantly share code, notes, and snippets.

@bejes
Created October 19, 2012 02:29
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bejes/3915923 to your computer and use it in GitHub Desktop.
Save bejes/3915923 to your computer and use it in GitHub Desktop.
Simple jquery plugin to insert spans in text and a defined character limit
/**
* Plugin function to insert spans into text:
* usage
* $(document).ready(function() {
* $('p.charcount').charSpanCount(); });
* or $(document).ready(function() {
* $('p.charcount').charSpanCount({'charcount' : 15});
* });
*/
(function($) {
$.fn.charSpanCount = function(options) {
var settings = $.extend({
// how many character before inserting span
'charcount' : 10
}, options);
var insert_span = function(data) {
var p_text = $(data).text();
var p_length = p_text.length;
var c = 0;
var charc, newstr = '';
while (c <= p_length) {
charc = p_text.charAt(c);
newstr = newstr + charc;
if (c % settings.charcount == 0 && c > 1) {
newstr = newstr + '<span>' + c + '</span>';
$(data).html(newstr);
}
c++;
}
};
return this.each(function() {
insert_span($(this));
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment