Solved

Highlighted text is picked up in TextArea on Button click

Posted on 2014-01-05
1
517 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
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
paypal ipn url 5 52
Web forms being filled out by bots 4 31
jquery selector - how to select all the td elements, that doesn't have an anchor element inside them 1 16
jqury 17 28
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now