Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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