Selection with HTML?

  • Thread starter Bruno Barberi Gnecco
  • Start date
B

Bruno Barberi Gnecco

Hi,

This question has been asked a few times in this group, but I
haven't been able to find an answer to any of the posts. window.getSelection()
returns the selected pure text, how to get it with the HTML tags?

Thanks a lot for any help.
 
M

Martin Honnen

Bruno said:
This question has been asked a few times in this group, but I
haven't been able to find an answer to any of the posts.
window.getSelection()
returns the selected pure text, how to get it with the HTML tags?

window.getSelection() gives you a selection object, not a string. You
can access a range in the selection and clone its contents, then append
to a div to extract the innerHTML e.g.
var selection = window.getSelection();
var range = selection.getRangeAt(0);
if (range != null) {
var div = document.createElement('div');
div.appendChild(range.cloneContents());
alert(div.innerHTML);
}

<http://www.xulplanet.com/references/objref/Selection.html>
 
B

Bruno Barberi Gnecco

Martin said:
window.getSelection() gives you a selection object, not a string. You
can access a range in the selection and clone its contents, then append
to a div to extract the innerHTML e.g.
var selection = window.getSelection();
var range = selection.getRangeAt(0);
if (range != null) {
var div = document.createElement('div');
div.appendChild(range.cloneContents());
alert(div.innerHTML);
}

<http://www.xulplanet.com/references/objref/Selection.html>

Thanks a lot, exactly what I needed.
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top