-8

HTML code--

<script language="javascript" type="text/javascript" src="js/jquery-1.4.2.min.js"></script>

<script language="javascript" type="text/javascript" src="js/jquery.field.selection.js"></script>

    <div id="copy">Copy</div>
        <textarea....id="t">

jquery---

$(docu.....
$('#copy').click(function(){
var range = $('#TextArea').getSelection();
alert(range.text);
});

});

When the #copy button is pressed the alert does not show the selected text in #t. It comes in blank.

I need the selected text from the textarea

genesis
  • 50,477
  • 20
  • 96
  • 125
X10nD
  • 21,638
  • 45
  • 111
  • 152
  • 1
    It helps if you actually ask a question. – Oded Sep 14 '11 at 12:48
  • try console.log or .debug and see what gets returned – Khurram Ijaz Sep 14 '11 at 12:52
  • Whats the idea with -4 on the Q? – X10nD Sep 14 '11 at 12:55
  • the question is extremely bad written. you aren't explaining what you've tried on you own, you don't say if any errors occur (and havn't posted the error-messages). your code is formatted badly and hard to read. anyway, i havn't downvoted this, but only because i have no votes left for today. – oezi Sep 14 '11 at 13:04
  • @oezi can you tell me what this sentence means - When the #copy button is pressed the alert does not show the selected text in #t. It comes in blank. – X10nD Sep 14 '11 at 16:33
  • @oezi can you tell me what this sentence means - I need the selected text from the textarea – X10nD Sep 14 '11 at 16:40

4 Answers4

1

getSelection is a method of the document, so you should do:

var range = document.getSelection();

also note that you'll have to use document.selection.createRange() in IE so everything gets a bit complicated.
take a look at this example for more information. you'll end up needing a function like this:

function getSelectedText(){
  if(document.all){
    var selection = document.selection;
    var newRng = selection.createRange();
    newRng.select();
    return newRng.htmlText;
  }else{
    return document.getSelection();
  }
}

wich should return the selected text and work in all major browsers.

EDIT:
just saw you're using some kind of jquery-plugin that (maybe) should make your code work. the problem is:

in your html, the id of the textarea is "t":

<textarea....id="t">

but in your javascript, you're trying to get the selection of id "TextArea":

$('#TextArea').getSelection();

please change the id of your textarea to "TextArea" (or the other way around) and see what happens.

oezi
  • 51,017
  • 10
  • 98
  • 115
1

Your code is not running because, this statement fails

var range = $('#TextArea').getSelection();

There is nothing as TextArea as ID in the markup you provided, so the script encounters an error and does not continue beyond it.

If you place the alert at the top part, I am sure the alert box will pop up. i.e

$('#copy').click(function(){
    alert(''); //this will work
    var range = $('#TextArea').getSelection();
    alert(range.text);
});
Starx
  • 77,474
  • 47
  • 185
  • 261
  • Did that, am getting the range.text blank (no value returned), but start point, end point all are returned – X10nD Sep 14 '11 at 16:34
  • @Jean, can you first show me where is `#Textarea` in your code... are you trying to refer to the textarea element – Starx Sep 14 '11 at 17:07
0

I'm not sure about the question, but if you need to get the textarea value, just use the val jQuery method:

http://api.jquery.com/val/

$('#copy').click(function(){
  var range = $('#t').val();
  alert(range);
});
Arnaud Leymet
  • 5,995
  • 4
  • 35
  • 51
0

Either you change your id="t" or you change #TextArea to #t to get the textarea you have in your html markup.

But I have no idea what plugin that you are using or what it want.

voigtan
  • 8,953
  • 2
  • 29
  • 30