Forum Moderators: open
when I paste it gives me :
"In the story so far, billionaire New York hedge fund proprietor Raj Rajaratnam has been charged,
Read more at : whatever.com ... "
<html>
<head>
<title>Using addRange to modify user selection</title>
<style type="text/css">
#addedMsg {
display: none;
}
</style>
</head>
<body>
Select any text. A key listener has been added to the window that
listens for the mouseup event, and appends a range to the
window selection. Note, the text area below makes it easier to test. Also,
because the listener is only listening for the mouseup event, it won't work
if the user selects text using another method (like with their browser's
"select all" method).
<script type="text/javascript" src="http://yui.yahooapis.com/2.8.0r4/build/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript">
(function () {
function modifySelectionText() {
var startNode = document.getElementById("addedMsg"),
endOffset = startNode.childNodes.length,
userSelection,
range;
if (window.getSelection) {
// This will work for Firefox
userSelection = window.getSelection();
// Append a range
range = document.createRange();
range.setStart(startNode, 0);
range.setEnd(startNode, endOffset);
userSelection.addRange(range);
}
else if (document.selection) {
// For IE, though not sure how to modify the selected text
userSelection = document.selection.createRange();
}
}
YAHOO.util.Event.on(document.body, 'mouseup', function () {
modifySelectionText();
});
})();
</script>
<form action="#">
<div>
<textarea rows="5" cols="50"></textarea>
</div>
<div id="addedMsg">This has been added</div>
</form>
</body>
</html>