Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

caret-pos

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

caret-pos

Locate the current position of the caret. A plain JavaScript version of Caret.js.

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4K
decreased by-20.36%
Maintainers
1
Weekly downloads
 
Created
Source

Build Status

Caret Pos

Get the position/offset of the caret/cursor from a textarea, contentedtiable, or an iframe body.

The reason for the iframe is that WYSIWYG editors are often inside of one.

This can also set the position in an editable field.

Caret.js

This is a port of ichord's Caret.js. The motivation for porting this was to drop the jQuery dependency and drop support for older browsers. This library won't work with any browser that doesn't have support for the Selection API. Check caniuse.com for browser support.

Thanks to ichord for all his hard work.

Usage

Simply import position and/or offset from cart-pos.

import { position, offset } from 'caret-pos';

Get

Use one of the functions to get the value.

const input = document.querySelector('.foo');
const pos = position(input); // { left: 15, top: 30, height: 20, pos: 15 }
const off = offset(input); // { left: 15, top: 30, height: 20 }

Set

Pass an integer to set the position in the input.

position(input, 11);

iframe context

In order to get the correct values for an iframe, we need to pass it in the settings so that it can get a reference to the iframe.

const frame = document.getElementById('iframe');
const body = frame.contentDocument.body;

const pos = position(body, { iframe: frame });
const off = offset(body, { iframe: frame });

You may also need to get the offset of the iframe to position things correctly. For this there is utility that you can use to get the offset.

import { getOffset } from 'caret-pos';

const frameOffset = getOffset(frame);
  off.left += frameOffset.left;
  off.top += frameOffset.top;

Keywords

FAQs

Package last updated on 26 Apr 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc