Get selected text in a textbox

html, input, javascript, textbox

Solution

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

Problem

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

Original source