You can create a dummy range to keep the same line as in the input text box.
On the keyboard, you update the content of the range and get a new length.
You better create a css rule with a text style definition for both text and text input, so you are sure that they have the same font style.
Your directive will look like this:
.html
<div edit-inline> <input type="text" value="hello world"> <span class="dummy">blbla</span> </div>
.js
app.directive("editInline", function(){ return function(scope, element, attr){ var elInput = element.find('input'); var elDummy = element.find('span'); var inputText = elInput.val(); elDummy.html(inputText); elInput.bind("keyup", function(){ var inputText = elInput.val(); elDummy.html(inputText); elInput.css('width', elDummy[0].offsetWidth + 'px'); }); } });
.css
input, .dummy { font-size: 12px; font-family: Arial; white-space:pre; } .dummy { visibility:hidden;
Here you can see plunker
source share