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
  • 26,636
  • 31
  • 113
  • 173

2 Answers2

1

You can use this code

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

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

Khoa Nguyen
  • 1,349
  • 7
  • 20
  • 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
1

I think this question has what you are looking for: jQuery Set Cursor Position in Text Area

Community
  • 1
  • 1
Matt Sherman
  • 8,053
  • 4
  • 36
  • 55