<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Yottaworks™ Studio &#187; Web</title>
	<atom:link href="http://yottaworks.net/tag/web/feed/" rel="self" type="application/rss+xml" />
	<link>http://yottaworks.net</link>
	<description>Ideas, illustrations, tutorials and footprints of Kael</description>
	<lastBuildDate>Sat, 20 Feb 2010 09:58:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Smart TextArea: scrollHeight in IE, Firefox, Opera and Safari</title>
		<link>http://yottaworks.net/smart-textarea-scrollheight-ie-firefox-opera-safari/</link>
		<comments>http://yottaworks.net/smart-textarea-scrollheight-ie-firefox-opera-safari/#comments</comments>
		<pubDate>Sat, 04 Jul 2009 13:05:03 +0000</pubDate>
		<dc:creator>Kael</dc:creator>
				<category><![CDATA[Designs]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://yottaworks.net/?p=1031</guid>
		<description><![CDATA[When we post comments on others' blogs or somewhere, there're often too many lines so that we should drag the scrollbar up and down to check our input, making it much inconvenient for us. 

So I wanna make a flexible textarea which can automatically adjust its own height with the content people input in, instead of showing a mechanical scrollbar. ]]></description>
			<content:encoded><![CDATA[<p>The scrollHeight is the distance between the top and bottom edges of the object&#8217;s content. But the height differs in various browsers.</p>
<p>For MSIE, the scrollHeight of a textarea is the real height of the content that you input just now including the paddings, while scrollHeight in Opera excludes the paddings.<br />
<img src="/wp-content/uploads/2009/07/scrollheight-ie-opera-tutorial.png" alt="" /></p>
<p>But in Firefox and especially in Safari, the scrollHeight turns out a bit weirdly. The scrollHeight of a textarea in firefox will be larger than or equal to the css height of the textarea, even if you have little input.<br />
<img src="/wp-content/uploads/2009/07/scrollheight-ff-safari-tutorial.png" alt="" /></p>
<h2 class="aboutTitle">Smart TextArea</h2>
<p>When we post comments on others&#8217; blogs or somewhere, there&#8217;re often too many lines so that we should drag the scrollbar up and down to check our input, making it much inconvenient for us.</p>
<p>So I wanna make a flexible textarea which can automatically adjust its own height with the content people input in, instead of showing a mechanical scrollbar.</p>
<p><strong>Concepts:</strong><br />
Set the height of the textarea equal to the scrollHeight.<br />
As the matter of Firefox and Safari, it doesn&#8217;t work when we want to reduce the height, so let&#8217;s have our idea fixed.</p>
<p>HTML:</p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1031code1'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p10311"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p1031code1"><pre class="html" style="font-family:monospace;">&lt;form&gt;
	&lt;div&gt;
		&lt;textarea name=&quot;comment&quot; id=&quot;comment&quot; rows=&quot;6&quot; tabindex=&quot;4&quot; cols=&quot;7&quot;&gt;
		&lt;/textarea&gt;
	&lt;/div&gt;
&lt;/form&gt;</pre></td></tr></table></div>

<p>We put a div out of the textarea, why?</p>
<p>To get the real height of the content, we have to set the textarea with no-height, so that we can get the desired value of scrollHeight.</p>
<p><strong>jQuery way:</strong></p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left2">Download <a href="http://yottaworks.net/wp-content/plugins/wp-codebox/wp-codebox.php?p=1031&amp;download=flexible-textarea.js">flexible-textarea.js</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p10312"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code" id="p1031code2"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'#comment'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">keyup</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> setTextareaHeight<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">function</span> setTextareaHeight<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #000066; font-weight: bold;">this</span>._browser <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">//get browser info.</span>
		<span style="color: #006600; font-style: italic;">//decide whether padding should be taken into account</span>
		<span style="color: #000066; font-weight: bold;">this</span>._browser <span style="color: #339933;">=</span> $.<span style="color: #660066;">browser</span>.<span style="color: #660066;">msie</span> <span style="color: #339933;">||</span> $.<span style="color: #660066;">browser</span>.<span style="color: #660066;">safari</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>._padding <span style="color: #339933;">=</span> setPreview._browser <span style="color: #339933;">?</span>
			<span style="color: #009900;">&#40;</span> parseInt<span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'#comment'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'padding-top'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span>
			parseInt<span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'#comment'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'padding-bottom'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> comHeight <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'#comment'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">//100 is the minimun height we want.</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> comHeight <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">100</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">//This is important!</span>
		<span style="color: #006600; font-style: italic;">//We fixed the height of the div,</span>
		<span style="color: #006600; font-style: italic;">//so that the page will not be twinkling</span>
		$<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'#comment'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'height'</span><span style="color: #339933;">,</span> $<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'#comment'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">//set the height to zero to get the real content height</span>
		$<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'#comment'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> comScrollHeight <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'#comment'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">scrollHeight</span> <span style="color: #339933;">-</span> <span style="color: #000066; font-weight: bold;">this</span>._padding<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> comScrollHeight <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">100</span> <span style="color: #009900;">&#41;</span>
		$<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'#comment'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span>comScrollHeight<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> comHeight <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">100</span> <span style="color: #009900;">&#41;</span>
		$<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'#comment'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	$<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'#comment'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'height'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'auto'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Just try to type in my textarea below. </p>
<p>If you have a better solution, <strong>you might as well share with us</strong>, which will be awfully appreciated!</p>
<ul>Tested with:
<li>Internet Explorer 6.0 &#8211; 8.0</li>
<li>Firefox 3.0 &#8211; 3.5</li>
<li>Safari 4.0</li>
<li>Opera 9.64</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://yottaworks.net/smart-textarea-scrollheight-ie-firefox-opera-safari/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
