javascript - Get HTML of selected content -


I have a UIWebView, I want to implement content functionality (can be text or text and images) so that Users email it
Is there a way to get HTML code for the given selection using Javascript? I tried the webkit's built-in clipboard but it is not working to select images. Maybe I am wrong, if there is a way please tell me.

  Miscellaneous category, frag, sel = window.getSelection (); If (sel.rangeCount) {range = sel.getRangeAt (0); Frag = range.cloneContents (); }  

This will give you a DocumentFragment with the content selected. You can cross the line of the piece using the normal DOM methods. If you have a textual HTML string, you can do the following:

  var div = document.createElement ("div"); Div.appendChild (frag); Warning (div.innerHTML);  

Note that this last part will not work if the selected content is & lt; Div & gt; (If, say, can not be placed inside the whole body or document was selected).


Comments