Append xhtml tags at arbitrary places in a text.
Posted on 2009-04-19
I'd like to be able to enclose snippets of existing text on a webpage at the click of a button. Kind of what jQuery's append does, but more arbitrary. It is my understanding that jQuery can append, say, <strong> tags to a pre-existing text without refreshing the page, but for that, the text has to be marked (i.e. it is possible to add <strong> to a <span>).
What I'd like to achieve is to append <strong> or <span> tags at arbitrary places, based on their offset (offset here means what window.getSelection().getRangeAt(0) can return).
<p span="line1">I want to ride my bicycle, I want to ride my bike.</p>
The function would be something like addStrong('line1', 15, 25); and it would transform the text like this:
<p span="line1">I want to ride <strong>my bicycle</strong>, I want to ride my bike.</p>