Solved

text input autoresize javascript

Posted on 2008-10-29
5
266 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
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…

943 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now