2

Every letter doesn't have it's own wrapper element, everything is inside paragraph.

And I am wondering if I can retrieve the position of the letter in string when I hover over it's parent element?

For example: <p>The quick brown fox jumps over the lazy dog</p> has innerText.length = 43, if I hover over f, it should return 16.

Any way to do this?

Thanks in advance!

tomsseisums
  • 13,168
  • 19
  • 83
  • 145
  • get the mouse position and the x/y offset of the

    and do some math. with a common font and fixed size it might work.

    – miki Sep 05 '11 at 16:38
  • 1
    With a fixed font - yes. Probably should've mentioned that no fixed font will be used and there will be varying sizes for the letters. – tomsseisums Sep 05 '11 at 16:42

2 Answers2

2

Options for you:

  1. To use Canvas (HTML5) and in particular its context.measureText(text).width. But this will give you position in single line text.
  2. To use cursor/range in contenteditable, see Get caret (cursor) position in contentEditable area containing HTML content for example.
Community
  • 1
  • 1
c-smile
  • 26,734
  • 7
  • 59
  • 86
  • Canvas won't cut it this time, too expensive for what am I building. About caret.. is there a way to apply it on hover? – tomsseisums Sep 05 '11 at 17:19
1

You'll have to put every letter into its own <span />. http://letteringjs.com/ might be useful to you, depending on what you're trying to do.

evan
  • 4,239
  • 1
  • 18
  • 18