hubcap caret — Get the caret position in an input

When to use

Get the cursor/caret position within an input or textarea. Use selection for document-level text selection.

Usage

hubcap caret <selector>

Arguments

ArgumentTypeRequiredDescription
selectorstringyesCSS selector of the input or textarea

Flags

None.

Output

FieldTypeDescription
startnumberStart index of the selection/caret position
endnumberEnd index of the selection/caret position

When no text is selected (caret at position 5):

{"start":5,"end":5}

When text is selected within the input (characters 3 through 10):

{"start":3,"end":10}

Errors

ConditionExit codeStderr
Element not found1error: element not found
Element does not support selection1error: element does not support selection
Chrome not connected2error: connecting to Chrome: ...
Timeout3error: timeout

Examples

Get the caret position in a text input:

hubcap caret '#search'

Focus an input, type some text, then check the caret position:

hubcap fill '#editor' 'Hello world' && hubcap caret '#editor' | jq -r '.end'

See also

  • selection - Get document-level text selection
  • focus - Focus an element
  • fill - Focus, clear, and type in one step
  • value - Get the value of an input