Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find absolute or relative position of last letter inside an input field?

Maybe this is a strange question. Please, check Bellow you will understand.

Empty input type text, width=200px :

[____________________________]

Filled input type text, width=200px :

[abcdefg_____________________]

If input left is 0 how to find the absolute or relative position where the g letter is???

When user enters some text I want to display under last letter a simple div...

like image 777
gadlol Avatar asked Jun 30 '11 14:06

gadlol


People also ask

What is position absolute and relative?

position: relative places an element relative to its current position without changing the layout around it, whereas position: absolute places an element relative to its parent's position and changing the layout around it.

What is relative positioning in CSS?

An element with position: relative; is positioned relative to its normal position. Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it to be adjusted away from its normal position. Other content will not be adjusted to fit into any gap left by the element.

What is absolute position CSS?

An absolutely positioned element is an element whose computed position value is absolute or fixed . The top , right , bottom , and left properties specify offsets from the edges of the element's containing block. (The containing block is the ancestor relative to which the element is positioned.)

How do I position a div relative to another?

We can make the dialog div positioned relative to the parent div by first making it a child of the parent div and then setting the parent position to relative. The parent is set to relative position and the dialog has absolute position. Now the dialog div is rendered over the parent div.


3 Answers

The text size hacks are OK, but you could alternatively use a visiblity: hidden span that moves your info div. HTML snippet follows:

<div><input type="text" value="hgello!!" onkeydown="document.getElementById('spacer').innerHTML = this.value;" /></div>
<div><span id="spacer" style="visibility: hidden;"></span>Character</div>

This way you can rely on the browser rendering the same font in roughly the same way into a span.

like image 134
Steven Dickinson Avatar answered Oct 14 '22 07:10

Steven Dickinson


I can only think of one way to reliably do this, and it's quite dirty.

1) Use a content editable div floated left: 2) surround that div with another with a width of 200, border, and onlick sets focus to the editable div 3) put the div you want to show after the last letter after the editable div, also floated left

Result: http://jsfiddle.net/tybro0103/zMezP/1/

Click on the box and start typing. The green box will move along with the cursor position.

In order to use this as a text field in a form you'll need to make a hidden input field. On form submit set the hidden field's value to editable div's inner html.

like image 41
tybro0103 Avatar answered Oct 14 '22 09:10

tybro0103


There is no built in "textWidth" param, sadly. However, a hack that will work pretty well: you can count the characters, guess at their width, and set your div "left" param equal to the character count * width (and make sure its absolutely positioned). something like:

var characterWidth = 6.8; //have to guess at this until it works...
var targetLocation = document.getElementById('yourInput').value.length * characterWidth;
document.getElementById('yourDiv').style.left = targetLocation + "px";

Run this script on a timer, every half second or so (or animate to the target location with jquery) and you should be in business.

Hope that helps.

As noted - this will only work if the font is monospaced and the user doesn't modify font size at all.

like image 1
Bosworth99 Avatar answered Oct 14 '22 09:10

Bosworth99