Is it possible to insert a div
after a certain amount of characters in a paragraph of text?
If I have a div
full of text, like so:
<div class="content">
Vivamus luctus urna sed urna ultricies ac tempor dui sagittis.
</div>
And I want to insert a div
after the 13th character:
Vivamus luctu<div>s urna sed urna ultricies ac tempor dui sagittis
Can I do this with jQuery/Javascript?
I am receiving the bounds for where I want to insert a div
from getSelection().extentOffset
which results in a number, say 13
which is where I want to insert the div
.
I was able to get this to partially work, like so:
$("div").on("mouseup", function () {
var start = window.getSelection().anchorOffset;
var end = window.getSelection().extentOffset;
console.log(start + ", " + end);
console.log($(".content").text().substring(start, end));
$('.output').html($('.content').html().substring(0, start) + '<span class="highlight">' + $('.content').html().substring(start, end) + "</span>" + $('.content').html().substring(end));
});
But that replaces the entire text when the bounds change, meaning that there can't be more than one section wrapped in a div
.