Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get selected text in a textbox

How can I get the character positions of the selected text in an HTML <input> textbox element? window.getSelection() doesn't work inside textboxes.

like image 685
Ant Avatar asked Mar 04 '10 08:03

Ant


1 Answers

If you're using jQuery, take a look at the jQuery Caret plugin: jCaret

// Get start pos in intput box with id="box1"
$("#box1").caret().start

// Get end pos
$("#box1").caret().end

// Get selected text
$("#box1").caret().text
like image 131
Ulf Lindback Avatar answered Oct 24 '22 04:10

Ulf Lindback