Solved

Highlighted text is picked up in TextArea on Button click

Posted on 2014-01-05
1
522 Views
Last Modified: 2014-01-25
I have a textarea with alot of text in ...however when I highlight(select) text and press the submit button...I only want that text to be carried back to the function for additional preocessing......Thank you
0
Comment
Question by:GlobaLevel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 500 total points
ID: 39758001
I found this http://jsfiddle.net/uES22/ and modified to http://jsbin.com/uZuzIlET/1/edit

function getSelectedText() {
    if (window.getSelection) {
        return window.getSelection().toString();
    } else if (document.selection) {
        return document.selection.createRange().text;
    }
    return '';
}

var b = document.getElementsByTagName('body')[0],
    o = document.getElementById('result');

b.onmouseup = function(e){
    var selText = getSelectedText(),
        targetElem = e.target.tagName.toLowerCase();
    if (selText && targetElem == 'textarea') {
        o.textContent =  selText ;
    }
};

Open in new window

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  <div>You can not select me</div>
  <textarea>Select me and it works. Beer is good food.</textarea><br>
  <div id="result"></div>
</body>
</html>

Open in new window

I think that solution may have started here http://www.codetoad.com/javascript_get_selected_text.asp
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
html form layout 4 38
Making Table Thru ASP Response.write 5 21
toggle content 12 30
GitHub 1 13
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question