Solved

text input autoresize javascript

Posted on 2008-10-29
5
268 Views
Last Modified: 2012-05-05
I need to make an input / textbox autoresize to the width of its contents.

dim w
w = document.getElementsByName("T1").textwidth("text")
document.getElementsByName("T1").width = w

does not work.
Thanks
0
Comment
Question by:sidwelle
  • 3
  • 2
5 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 22834831
Give your input box an id="T1" as well and then try:

dim w
w = Len( document.getElementById("T1").value )
document.getElementById("T1").size = w
0
 

Author Comment

by:sidwelle
ID: 22835116
dim w
w = Len( document.getElementById("T1").value )
document.getElementById("T1").width = (w * 7) + 7

This kinda works, but not all the time.  I was looking for some auto feature.
Or an AutoSize would be cool ...
0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 22835303

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">
 
<html>
<head>
<title></title>
<script type="text/vbscript">
Sub resizeBox
	Dim w
	w = Len( document.getElementById("T1").value ) + 1
	document.getElementById("T1").size=w
End Sub
</script>
</head>
<body>
	<input type="text" size=1 name="T1" id="T1" onkeypress="resizeBox "/>
</body>
</html>

Open in new window

0
 

Author Comment

by:sidwelle
ID: 22835585
Any way to do this automatic ?, the kerning fonts leave a little to be desired.
0
 
LVL 82

Expert Comment

by:hielo
ID: 22835766
>>Any way to do this automatic ?,
No. You will need to use scripting as shown previously
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question