Range / Selection get HTML

I have this fiddle: http://jsfiddle.net/Uddaa/

Currently, what he does is when you click on the text, he will do a warning with the text to this point. What can I do to get html to this point, not just text?

JavaScript:

$(".content").on("click", function () { getBefore(); }); function getBefore() { var sel = document.getSelection(); var off = sel.anchorOffset; var ran = sel.getRangeAt(0); ran.setStart($(".content").get(0), 0); alert(ran.toString()); } 

HTML:

 <div class="content"> <p><b>Click</b> anywhere <u>within <em>the</em> text</u> to to see the code up to that point.</p> </div> 
+8
javascript jquery html
source share
2 answers

ran.cloneContents().childNodes[1].innerHTML gives you the full html.

See jsfiddle

+8
source share

You can use the Rangy library to do this:

 rangy.getSelection().toHtml() 

Script example

+1
source share

All Articles