admin管理员组文章数量:1319466
I am trying to create a text box that grows every time a letter is added to it by the width of the character. This is what I'm trying. It doesn't work. If you've got a hint, please share!
JavaScript:
function MakeWidth(el) {
el.style.width = parseFloat(el.value.clientWidth) }
HTML:
<input id="Test" type="text" style="width: 10px;" onkeyup="MakeWidth(this)"/>
I am trying to create a text box that grows every time a letter is added to it by the width of the character. This is what I'm trying. It doesn't work. If you've got a hint, please share!
JavaScript:
function MakeWidth(el) {
el.style.width = parseFloat(el.value.clientWidth) }
HTML:
<input id="Test" type="text" style="width: 10px;" onkeyup="MakeWidth(this)"/>
Share
Improve this question
asked Nov 13, 2011 at 19:52
Michael SwartsMichael Swarts
3,9218 gold badges36 silver badges46 bronze badges
3 Answers
Reset to default 8Try size
& length
instead of clientWidth
& width
http://jsfiddle/eVd9b/5/
function MakeWidth(el) {
el.size = parseInt(el.value.length);
}
which should be suitable for your needs.
It may be more useful to use the size
attribute.
<input id="Test" type="text" size="1" onkeyup="MakeWidth(this)"/>
<script>
function MakeWidth(el) {
el.size = el.value.length
}
</script>
That's a bit tricky since you don't necessarily know the width of each character. One little trick that es to mind is that you could try creating a hidden div with the typed character, grab it's width, add it to the textbox width and destroy the hidden div.
本文标签: javascriptGrowing text box based on width of characters inputStack Overflow
版权声明:本文标题:javascript - Growing text box based on width of characters input - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742062594a2418654.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论