find start and end of highlighted text in string

C

charliefortune

I am creating a little html editor, and I want to highlight a chunk of
unformatted text, click a <button> and put a <p> and the beginning and
a </p> at the end. I assume there is a DOM element relating to this,
can anybody help please ? Thanks
 
U

Une Bévue

charliefortune said:
I am creating a little html editor, and I want to highlight a chunk of
unformatted text, click a <button> and put a <p> and the beginning and
a </p> at the end. I assume there is a DOM element relating to this,
can anybody help please ? Thanks

Saying the highlighted text is s :
var p=document.createElement('p');
p.appendChild(document.createTextNode(s));
document.body.appendChild(dp)

or if you have an element with a given id "elt_id" wehere u want to put
your <p>blahblahblah</p> it is in this case :

<same as above until last line>
document.getElementById('elt_id').appendChild(p);

that's all ;-)

obviously the selected text MUST BE well balance, that's to say behaves
as xml... (if some tags in it)

how do u get the selected text ? from javascript ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top