![]() |
get user selected HTML text
On IE you can use selection.createRange().htmlText to get the user selected HTML text. Other browsers like Safari and Firefox only have getSelection() which can only return unformated plain text, and there is nothing for HTML text.
Does anyone have a method created to get user selected html text on all browsers? Thanks |
Use this:
Code:
function getSel() { |
getSelection only returns plain text not HTML text. I need HTML text. That's my challenge.
|
This works in IE:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
There is no built-in methods for this on non-IE browsers. What has to be done is a a custom method of getting the start and end position of the getSelected text and then return the innerHTML in a substring. That should do it :)
Thanks! |
This doesn't work, but I need some sleep. Maybe you can arrive at a solution from some of the methods I used:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
look no further:
Code:
function selHTML() { |
Range.surroundContents - didn't occur to me ;)
Nice job. Perhaps it may be slightly better to run object detection on the specific functions we're going to use though? Also if there is more than one range selected by the user, the code will only return the first range. Also changed "p" to "span" so it wouldn't interfere with the page layout. Code:
function getSelectionHTML() |
Yes the surroundContents was the creativity I was looking for :) Thanks!
I rewrote it to remove the added element, and now its working perfectly (you must pass the HTML text's parent element ): Code:
function getSelectedHTML( element ) {When you select HTML like this (underlined = selected text): This is some <strong>html text</strong> that has been selected. it returns "<strong>text</strong> that has been" when it should be returning "text</strong> that has been" Any ideas? |
You don't need to pass anything:
Code:
function getSelectionHTML() |
I don't think you should put the content into a new <p> element using surroundsContents. You should just use extractContents.
|
Thanks, but that doesn't always work. Passing the element is more reliable :)
What about the selection issue I mentioned. Any ideas? It seems to add the opening tag to the returned selected html when you select from before to after a close tag. Any ideas? Thanks! |
Quote:
Quote:
|
Quote:
-Itsallkizza Thanks for the advise, I'll implement it all and post it once it's debugged.. |
Code:
var range = window.getSelection().getRangeAt(0); |
| All times are GMT +1. The time now is 10:47 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.