admin管理员组文章数量:1279243
How to calculate the width of an input HTML element so that it matches the size of its content ? I already update an input on the fly as the user types :
<input type='text' onkeydown='this.size=this.value.length' />
However, this does not seem pletely correct because it does not take into account the fact that some characters are longer than others :
- I will get more and more whitespace if I type only some "l" characters
- the size will be insufficient if I type only some "w" characters
How to proceed?
PS: Why I want to do this (already answered this in a answer that was deleted)? I have sentences in which I have to replace the bracket content by inputs (ex: [user] is [years] old). I have no idea what the sentence can be, so I do not know an adequate length for the inputs, and I would like to keep it readable on one line (avoiding too much whitespace).
How to calculate the width of an input HTML element so that it matches the size of its content ? I already update an input on the fly as the user types :
<input type='text' onkeydown='this.size=this.value.length' />
However, this does not seem pletely correct because it does not take into account the fact that some characters are longer than others :
- I will get more and more whitespace if I type only some "l" characters
- the size will be insufficient if I type only some "w" characters
How to proceed?
PS: Why I want to do this (already answered this in a answer that was deleted)? I have sentences in which I have to replace the bracket content by inputs (ex: [user] is [years] old). I have no idea what the sentence can be, so I do not know an adequate length for the inputs, and I would like to keep it readable on one line (avoiding too much whitespace).
Share Improve this question edited May 31, 2012 at 13:38 JB Hurteaux asked May 31, 2012 at 12:36 JB HurteauxJB Hurteaux 4,5586 gold badges34 silver badges35 bronze badges 5- 2 Why do you want this in the first place? Maybe there's an easier workaround – Pekka Commented May 31, 2012 at 12:39
- onkeydown wouldn't catch pasting via a mouse click – user391538 Commented May 31, 2012 at 12:59
- @Pekka Question edited. See above. – JB Hurteaux Commented May 31, 2012 at 13:39
- @TonyBlundell I know, need to fine-tune this. Thanks anyway. – JB Hurteaux Commented May 31, 2012 at 13:40
- Ah, I see. There is a duplciate of this somewhere on SO that IIRC even had a nice code sample, but I can't find it right now... – Pekka Commented May 31, 2012 at 13:40
4 Answers
Reset to default 4You could use a (hidden) canvas and the measureText()
method of the context to get your string's width.
EDIT:
Looks fast enough.
First, define some CSS...
input,
#input-helper {
display: inline;
font-size: 14px;
font-family: serif;
line-height: 16px;
}
#input-helper {
position: absolute;
top: -10000px;
}
...then use some JavaScript...
var div = document.createElement("div");
div.id = "input-helper";
document.body.appendChild(div);
var input = document.querySelector("input");
input.addEventListener("keyup", function() {
div.textContent = this.value;
input.style.width = div.offsetWidth + "px";
});
jsFiddle.
You will want to choose a reasonable start width for your input
element too.
If using jQuery is not a problem, here is a demo I put together on jsFiddle. It uses an Autoexpand.js
file that does what you want. Check out the last example in the fiddle.
Some specifics:
- It's based on
.keyup
and.keypress
for the fastest response possible. - It takes into account the HTML markup that's pasted into the box. Things like linebreaks are dealt with.
- The source file shows smart processing by taking everything about the font into consideration.
Also included in the jsFiddle are links to download a pastebin version of the fiddle since jsFiddle Sandbox breaks in IE8. That said, it also works in IE7 too!
First : Add this div where you want
<div id="calcsize" style="display:none;"></div>
Second : Use this function
function getWidthof(txt)
{
$('#calcsize').empty().append(txt);
return $('#calcsize').width();
}
本文标签:
版权声明:本文标题:javascript - How to calculate the width of an input HTML element so that it matches the size of its content? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741257553a2366974.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论