Forum Moderators: open

Message Too Old, No Replies

Expand textarea

         

music_man

10:56 am on May 8, 2006 (gmt 0)

10+ Year Member



I'm just editing a script posted by birdbrain:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>textarea expander</title>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
</head>
<body onload="initiateWidth()">

<script type="text/javascript">
//<![CDATA[
var df=document.forms;
function initiateWidth() {
df[0][0].style.width=df[0][1].offsetWidth+"px"
}
function expandtextarea(which) {
if(which==0) {
df[0][1].rows=df[0][1].rows+1;
df[0][0].style.height=df[0][1].offsetHeight+25+"px";
}
else {
df[0][1].cols=df[0][1].cols+1;
df[0][0].style.width=df[0][1].offsetWidth+"px";
}
}
//]]>
</script>

<form id="theForm" action="#">

<fieldset style="border: 0;">
<textarea rows="12" cols="60">
</textarea>
<div>
<br />
<input type="button" value="Add row" onclick="expandtextarea(0)"/>
<input type="button" value="Expand" onclick="expandtextarea(1)" />
</div>
</fieldset>
</form>
</body>
</html>

Only if you have another field in the form, it will expand the other field instead.

var df=document.forms; the issue?

birdbrain

4:05 pm on May 8, 2006 (gmt 0)



Hi there music_man,

df[0] refers to the first form on the page.
df[0][0] refers to the first element within the first form.

This means that df[0][0] refers to the fieldset element and df[0][1] to the textarea element.
If you add elements to the form then you will have to adjust the references accordingly.
Also note that the script should be placed within the head section not the body.

I hope that this helps. ;)

birdbrain

music_man

10:01 pm on May 8, 2006 (gmt 0)

10+ Year Member



Thanks for that! Why does the script need to go in the head?

Fotiman

10:06 pm on May 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Technically, it doesn't need to. But many people find it much easier to maintain if the script is always defined in the head of the document (don't have to search through the body for it). It's a little cleaner and since the script is not really "content", it's a little more semantically correct to put it in the <head> since that usually just contains a bunch of meta information anyway.

music_man

10:12 pm on May 8, 2006 (gmt 0)

10+ Year Member



I saved it as a .js file and put it in another directory. I also added the buttons to the js file so that if a user does not have javascript on they are not faced with buttons they cannot use.