2
 <script type="text/javascript">
        /* run on document load **/
        function disp() {
            //var text = document.getElementById('TextArea1').value;

            var text = document.getElementById('MainContent_TextArea1').value;
            //alert('a')

            if (text == null)
            {
                return false;
            } 
            else
            {
                var t = text;
          var t = text.substr(text.selectionStart, text.selectionEnd -text.selectionStart);
               document.getElementById('displayval').value = t;
                 alert(t)
                }
            }
  </script>
    <div>
    <input id="TextArea1" runat="server" type="text"/>
    <INPUT type="button" onclick= "disp()" visible="true" value="Show"/>
    <INPUT type="text"  id ="displayval" visible="true" />
    </div>

Here I am trying to display the Text which is selected by user. But unfortunately in var t = text.substr(text.selectionStart, text.selectionEnd -text.selectionStart); getting error. It is not working. Any wrong in this code. Please help me with correcting this..

kiran
  • 153
  • 1
  • 3
  • 15

1 Answers1

1
<script type="text/javascript">
    function disp() {
        var txtArea = document.getElementById('TextArea1');
        var start = txtArea.selectionStart;
        var finish = txtArea.selectionEnd;
        var sel = txtArea.value.substring(start, finish);
        document.getElementById('displayval').value = sel;
        alert(sel);
    }
</script>
<div>
    <input id="TextArea1" runat="server" type="text"/>
    <input type="button" onclick= "disp()" visible="true" value="Show"/>
    <input type="text"  id ="displayval" visible="true" />
</div>
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Hi Tim, My Tricky manager has changed the requirement again. Now He doesn't want to display it through alert. He wants to display the selected text in Text box which comes from c#. So we need to bind the selected text to text box. How can we do this. – kiran Mar 24 '16 at 06:58
  • Please open a new question and ask there. – Tim Biegeleisen Mar 24 '16 at 07:00