1

Update: I am sorry for not making it clear - i do not mean getting the text, i meant emulate the effect as if the user had double-clicked on a word in the text area.

Supposed i have a textarea that looks like this:

<textarea id="r">Hello, this is random text. i want to select -> THIS </textarea>

I want to select the last 4 characters of this textarea, preferably w jQuery

How do i do so?

meow
  • 27,476
  • 33
  • 116
  • 177

1 Answers1

1

You can use this code

$('#r').val().substr(-4);

http://jsfiddle.net/ynhat/nc4pk/

Khoa Nguyen
  • 1,319
  • 7
  • 21
  • Way more elegant solution. Only thing I suggest is trimming the data, because he says the last 4 characters, and I think by error has the extra space. +1 – Loktar Mar 18 '11 at 03:51
  • Heres an updated with space trimming so it always gets the last 4 characters. http://jsfiddle.net/loktar/nc4pk/1/ – Loktar Mar 18 '11 at 03:54
  • Thanks. I think our answer is not what he wants. He want to highlight the text, not just get the text. – Khoa Nguyen Mar 18 '11 at 03:57