Coder Social home page Coder Social logo

kir / js_cursor_position Goto Github PK

View Code? Open in Web Editor NEW
35.0 3.0 3.0 200 KB

A small library which allows to detect cursor position in Javascript textarea

Home Page: http://kirblog.idetalk.com/2010/03/calculating-cursor-position-in-textarea.html

License: MIT License

JavaScript 95.36% CSS 4.64%

js_cursor_position's Introduction

Deprecation notice

This library is no longer maintained, you may want to take a look at this solution instead: https://github.com/component/textarea-caret-position

JavaScript cursor position in a textarea or input

What

To obtain the text cursor position in a <textarea> or an <input> text field. Cursor position is provided either in characters, or in pixels relative to top-left corner of the text area.

Why

The library can be used for positioning of a completion popup window in a <textarea> or <input> text field right near the caret. Given that long text is wrapped in <textarea>s, the task is not very trivial.

Alternative solution

There is another, more recent library which uses the same principle and may have less (or different) bugs: https://github.com/component/textarea-caret-position

How

The library tries to model text wrapping in the textarea and to find out relative cursor position. The results are not 100% accurate, but really close to it. Tested against IE8+, Chrome, Opera, Firefox as of 2012.

Usage

var padding = 3;
var positioner = new maxkir.CursorPosition(textarea_element, padding);
alert(positioner.getCursorCoordinates());  // [x, y] position of cursor in textarea in characters
alert(positioner.getPixelCoordinates());   // [x, y] position of cursor in textarea in pixels

See also the functional test under [test/cursor_position_test.html]<test/cursor_position_test.html>

Dependencies

None, plain JavaScript code.

-- Copyright (c) 2010-2012 Kirill Maximov, released under the MIT license

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.