<?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>Alessandro Melandri &#187; HTML</title>
	<atom:link href="http://www.melandri.net/tag/html/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.melandri.net</link>
	<description>J2EE Developer &#38; Photography enthusiast</description>
	<lastBuildDate>Tue, 27 Jul 2010 19:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Add maxlength functionality to textarea</title>
		<link>http://www.melandri.net/2009/05/28/add-maxlength-funcionality-to-textarea/</link>
		<comments>http://www.melandri.net/2009/05/28/add-maxlength-funcionality-to-textarea/#comments</comments>
		<pubDate>Thu, 28 May 2009 07:54:48 +0000</pubDate>
		<dc:creator>amelandri</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://localhost/blog/?p=23</guid>
		<description><![CDATA[The textarea field doesn&#8217;t have a maxlength attribute like text fields, so you need Javascript to get the same functionality: function getMaxLength&#40;textarea,maxlength&#41;&#123; &#160; if &#40;textarea.value.length &#38;gt; maxlength&#41; textarea.value = textarea.value.substring&#40;0,maxlength&#41;; &#160; updateCounter&#40;textarea.value.length,maxlength&#41;; &#125; &#160; function updateCounter&#40;currentCount,maxlength&#41;&#123; document.getElementById&#40;'counter'&#41;.innerHTML = currentCount + &#34;/&#34; + maxlength; &#125; Now modify the textarea like this and you are done: &#60;form&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>The textarea field doesn&#8217;t have a maxlength attribute like text fields, so you need Javascript to get the same functionality:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> getMaxLength<span style="color: #009900;">&#40;</span>textarea<span style="color: #339933;">,</span>maxlength<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>textarea.<span style="color: #660066;">value</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> maxlength<span style="color: #009900;">&#41;</span>
    textarea.<span style="color: #660066;">value</span> <span style="color: #339933;">=</span> textarea.<span style="color: #660066;">value</span>.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>maxlength<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  updateCounter<span style="color: #009900;">&#40;</span>textarea.<span style="color: #660066;">value</span>.<span style="color: #660066;">length</span><span style="color: #339933;">,</span>maxlength<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;">function</span> updateCounter<span style="color: #009900;">&#40;</span>currentCount<span style="color: #339933;">,</span>maxlength<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'counter'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> currentCount <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;/&quot;</span> <span style="color: #339933;">+</span> maxlength<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now modify the textarea like this and you are done:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;form&gt;
  &lt;textarea onkeyup=&quot;return getMaxLength(this,500)&quot; cols=&quot;60&quot; rows=&quot;10&quot;&gt;&lt;/textarea&gt;
&lt;div id=&quot;counter&quot;&gt;0/500&lt;/div&gt;
&lt;/form&gt;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.melandri.net/2009/05/28/add-maxlength-funcionality-to-textarea/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
