Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

First line of default text in a <textarea> starts at the center?

Status
Not open for further replies.

other3

Programmer
Jul 30, 2002
92
US
If some would let me know how to get around this that would be great? Can't see any <textfield> attributes that need to be set.

Charlie
 
This will center the first line of your default text. You should be able to figure out how to do more lines if you want.

<html>
<body onload=&quot;centerIt()&quot;>
<script language=&quot;javascript&quot;>
function centerIt()
{
var box = document.getElementById('txtBox');
var defaultString = &quot;Hello World&quot;;

//determine how many spaces move over to center
var leftPos = (box.cols - defaultString.length)/2;
var newString = &quot;&quot;;

//add that mant spaces to the beginning of the new string
for (var i=0; i < leftPos; i++)
newString += &quot; &quot;;

//If you don't put a space after the string, strings containing
//no strings will not be printed (don't know why)
newString += defaultString + &quot; &quot;;
box.innerHTML = newString;
}
</script>
<form method=&quot;POST&quot;>
<p><textarea rows=&quot;2&quot; name=&quot;txtBox&quot; id=&quot;txtBox&quot; cols=&quot;20&quot;></textarea></p>
</form>
</body>
</html>

If you want all text in the text area to be centered use:
<textarea rows=&quot;2&quot; name=&quot;txtBox&quot; id=&quot;txtBox&quot; cols=&quot;40&quot; style=&quot;text-align:center&quot;></textarea>

Hope that helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top