Saturday, 19 July 2014

Copy Highlighted Text In TextArea or TextBox Using JavaScript In Asp.Net

7/19/2014 - By Pranav Singh 0

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 

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top