Return HTML from a user selection
Posted
by
Cruinh
on Stack Overflow
See other posts from Stack Overflow
or by Cruinh
Published on 2011-01-10T23:31:11Z
Indexed on
2011/01/10
23:53 UTC
Read the original article
Hit count: 145
JavaScript
I have the following, very simple html page...
<html>
<head>
<script type="text/javascript">
function alertSelection()
{
var selection = window.getSelection();
var txt = selection.toString();
alert(txt);
}
</script>
</head>
<body>
This is <span style="background-color:black;color:white">the</span> text.
<div style="background-color:green;width:30px;height:30px;margin:30px" onmouseover="alertSelection()">
</body>
</html>
When I select the entire first line and mouseover the square, I get an alert with "This is the text.".
How would I fix this so the the span tag or any other selected HTML isn't stripped out of the alert message?
© Stack Overflow or respective owner