Created
October 19, 2012 02:29
-
-
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
This file contains hidden or 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
/** | |
* 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