This article will explain your how you can copy highlighted text
In textarea or textbox using JavaScript In Asp.Net. This article will show you
not only copy the text but if you are highlighting any bold or italic text then
you will get the html text.
So for this article first we will create a new asp.net
application and add the below code in it.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CopyHilightedTextHTML.aspx.cs"
Inherits="demoasp_net.CopyHilightedText" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Copy
Highlighted Text In TextArea or TextBox Using JavaScript In Asp.Net</title>
<script language="javascript">
function
getSelectionHtml() {
var
selectedData = "";
/*For
other browser*/
if
(typeof window.getSelection != "undefined") {
var
seldata = window.getSelection();
if
(seldata.rangeCount) {
var
container = document.createElement("div");
for
(var i = 0, len = seldata.rangeCount; i <
len; ++i) {
container.appendChild(seldata.getRangeAt(i).cloneContents());
}
selectedData =
container.innerHTML;
}
/*For
IE*/
} else
if (typeof
document.selection != "undefined")
{
if
(document.selection.type == "Text")
{
selectedData =
document.selection.createRange().htmlText;
}
}
/*For
displaying the selected text in textarea*/
document.getElementById("txtselecteddata").value =
selectedData;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div style="text-align: center">
This is a <em>demo
text</em> to <strong>diaplay</strong> the <u>hilighted</u> text
with html</div>
<div align="center">
<p>
<textarea rows="4" cols="45" id="txtselecteddata"></textarea>
</p>
<input onclick="javascript:getSelectionHtml()" type="button"
value="Press to
copy the highlighted text"
name="btnCopy">
</div>
</div>
</form>
</body>
</html>
|
In above code we have used html code for copy and display
the highlighted text.
<script language="javascript">
function
getSelectionHtml() {
var
selectedData = "";
/*For
other browser*/
if
(typeof window.getSelection != "undefined") {
var
seldata = window.getSelection();
if
(seldata.rangeCount) {
var
container = document.createElement("div");
for
(var i = 0, len = seldata.rangeCount; i <
len; ++i) {
container.appendChild(seldata.getRangeAt(i).cloneContents());
}
selectedData =
container.innerHTML;
}
/*For
IE*/
} else
if (typeof
document.selection != "undefined")
{
if
(document.selection.type == "Text")
{
selectedData =
document.selection.createRange().htmlText;
}
}
/*For
displaying the selected text in textarea*/
document.getElementById("txtselecteddata").value =
selectedData;
}
</script>
|
Now run the application to view the output.
DOWNLOAD
0 comments:
Please let me know your view