You can go to . html callback and add any tag to text e.g.
$('.first_ltr p').html(function (index, html) {
return '<span>' + html.slice(0, 1) + '</span>' + html.slice(1);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="first_ltr">
<p>This is a Test </p>
</div>
Run codeHide resulthtml.slice(0, 1) - returns the first letter in a string
html.slice(1) - returns a string without the first letter
Update
I wrote a small plugin, you can try it
$.fn.wrapText = function (tag, position) {
var $contaiter = $('<div />');
this.html(function (index, html) {
var start = position;
end = position + 1;
if (position > html.length) {
position = 0;
}
$contaiter.html($('<' + tag + '/>').html(html.slice(start, end)));
return [html.slice(0, start), $contaiter.html(), html.slice(end)].join('');
});
};
$('.first_ltr p').wrapText('span', 0);
$('.last_ltr p').wrapText('div', 13);
Example
source
share