Wednesday, November 19, 2014

Getting Selected Text of TextArea Using JavaScript

Getting Selected Text of TextArea Using JavaScript:

     You can get the selected text of a textarea using pure javascript. The below example shows how to achieve it. The example below tested with chrome, mozilla and ie with the following specified versions.Chrome version 38.0.2125.111, ie version 11.0.9600, mozilla version 32.0.3


<script type="text/javascript">
function getSelectedText() 
var txtArea = document.getElementById("textarea1");
var selectedText;   
if (txtArea.selectionStart != undefined)
    var startPosition = txtArea.selectionStart;
    var endPosition = txtArea.selectionEnd;
     selectedText = txtArea.value.substring(startPosition, endPosition);
alert("You selected :" + selectedText);
<textarea id="textarea1" onselect="getSelectedText()" cols="75"  rows="30">

The screen will look like the following.

Getting Selected Text of TextArea Using JavaScript
Figure 1

You can download the source code from here


  1. I just found this blog and have high hopes for it to continue. Keep up the great work, its hard to find good ones. I have added to my favorites. Thank You. Yellow Pages Website Scraper Software

  2. Great write-up, I am a big believer in commenting on blogs to inform the blog writers know that they’ve added something worthwhile to the world wide web!.. Ad Verification Proxies

  3. I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit. my blog

  4. Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our. trafficize reviews

  5. Your article has piqued a lot of positive interest. I can see why since you have done such a good job of making it interesting. EAbuilder Reviewed

  6. Thanks for the blog loaded with so many information. Stopping by your blog helped me to get what I was looking for. Lehigh Valley Database Developers

  7. This programming language is very helpful for developers and through this, they can develop different softwares and earn a lot of money. Dissertation editing services.

  8. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. rolet online