Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8835273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:13:21+00:00 2026-06-14T09:13:21+00:00

UPDATE : duplicate of Get cursor or text position in pixels for input element

  • 0

UPDATE: duplicate of Get cursor or text position in pixels for input element.

TL; DR – use the incredibly lightweight and robust textarea-caret-position Component library, which now supports <input ype="text"> as well. Demo at http://jsfiddle.net/dandv/aFPA7/


Is there a way to know where the caret is inside an HTML text field?

<input type='text' /> 

I would like to position in pixels (and reposition) a div depending on the position of the caret.

Note: I don’t want to know the position in characters or in a <textarea>. I want to know position in pixels in an <input> element.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:13:22+00:00Added an answer on June 14, 2026 at 9:13 am

    Using an invisible faux element

    You can accomplish this by using an absolutely positioned invisible (using visibility not display) faux element that has all the same CSS properties as your input text box (fonts, left borders and left padding).

    This very short and easy to understand JSFiddle is a starting point how this script should be working.
    It works in Chrome and Firefox as is. And it seems it should be working in IE9+ as well.

    Internet Explorer 8 (and down) would need some additional code to get caret position from start of text within input text box. I’ve even added a meter at the top to show a line every 10 pixels so you can see whether it measures correctly or not. Mind that lines are at 1, 11, 21,… pixel positions.

    What this example does it actually takes all the text in text box up to caret position and puts it inside the faux element and then measures its width in pixels. This gets you offset from left of text box.

    When it copies text to faux element it also replaces normal spaces with non-breaking ones so they actually get rendered otherwise if you’d position caret right after space you’d get wrong position:

    var faux = $("#faux");
    $("#test").on("keyup click focus", function(evt) {
        // get caret offset from start
        var off = this.selectionStart;
    
        // replace spaces with non-breaking space
        faux.text(this.value.substring(0, off).replace(/\s/g, "\u00a0"));
    });​
    

    Mind that faux’s right dimensions

    • padding
    • border
    • margin

    have been removed in order to get correct value, otherwise element would be too wide. Element’s box has to end right after the text it contains.

    Caret’s position in pixels from start of input box is then easily gotten from:

    faux.outerWidth();
    

    The problem

    There is one problem though. I’m not sure how to handle situation when text within text box is scrolled (when too long) and caret isn’t at the very end of text but somewhere in between… If it’s at the end then caret position is always at maximum position possible (input width less right dimensions – padding, border, margin).

    I’m not sure if it’s possible to get text scroll position within text box at all? If you can then even this problem can be solved. But I’m not aware of any solution to this…

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: jQuery: get the file name selected from <input type=file /> I have
Possible Duplicate: iOS: Get location update every n minutes How can I get location
Possible Duplicate: PHP Regex Get Text Between BBCode Tags I have a text like:
I have a mysql ON DUPLICATE KEY UPDATE statement that is running successfuly. Although
Is it possible to INSERT...ON DUPLICATE KEY UPDATE into a table on the current
This question here: MySQL: INSERT IGNORE or ON DUPLICATE KEY UPDATE with checking multiple
I've simple question. I'm trying to on duplicate key update supplier name and price
Update: This question is a duplicate of Are there any programming languages targeting PHP,
Possible Duplicate: Do NSUserDefaults persist through an Update to an app in the Appstore?
Possible Duplicate: SQL ORDER BY total within GROUP BY UPDATE: I've found my solution,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.