menu

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

Example:

     <html>
       <head>
<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);
}
</script>
</head>
<body>
<textarea id="textarea1" onselect="getSelectedText()" cols="75"  rows="30">
</textarea>
</body>
      </html>

The screen will look like the following.


Getting Selected Text of TextArea Using JavaScript
Figure 1

You can download the source code from here

9 comments:

  1. Well, that’s the first time on your blog but I really like your post.

    I have my own blog where I post about E Scooter App Development and much more take a look.

    ReplyDelete
  2. 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

    ReplyDelete
  3. 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

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

    ReplyDelete
  5. 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

    ReplyDelete
  6. 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

    ReplyDelete
  7. 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

    ReplyDelete
  8. 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.

    ReplyDelete
  9. This article is worth sharing, so inspiring and full of information. Thankyou for sharing this!!

    meal kit delivery services market

    ReplyDelete