<?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; textarea</title>
	<atom:link href="http://www.melandri.net/tag/textarea/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>Textarea char counter, a jQuery plugin</title>
		<link>http://www.melandri.net/2009/10/20/textarea-char-counter-a-jquery-plugin/</link>
		<comments>http://www.melandri.net/2009/10/20/textarea-char-counter-a-jquery-plugin/#comments</comments>
		<pubDate>Tue, 20 Oct 2009 16:08:34 +0000</pubDate>
		<dc:creator>amelandri</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[textarea]]></category>

		<guid isPermaLink="false">http://www.melandri.net/?p=642</guid>
		<description><![CDATA[Some weeks ago I received a customer request to add a character counter to a textarea field and I made it using &#8220;classic&#8221; Javascript. Now that I&#8217;m learning jQuery and have some spare time, I tryed to transform my custom function to a jQuery plugin. This is my first attempt to build a jQuery plugin [...]]]></description>
			<content:encoded><![CDATA[<p>Some weeks ago I received a customer request to add a character counter to a textarea field and I made it using &#8220;classic&#8221; Javascript. Now that I&#8217;m learning <a href="http://www.jquery.com">jQuery</a> and have some spare time, I tryed to transform my custom function to a jQuery plugin.</p>
<p>This is my first attempt to build a jQuery plugin and maybe there&#8217;s already another plugin that does the same thing but it have been a really nice exercise.</p>
<p>Check out the plugin code and some more details after the break.</p>
<p><span id="more-642"></span></p>
<p>This plugin will give you the method <em>setCounter([maxLength])</em>. If you call it on a textarea field you will get a counter that gets updated every time the user write a character.</p>
<p>The <em>maxLength</em> parameter is not mandatory so if it&#8217;s undefined you will get a simple character counter like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;textareaId_counter&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;length&quot;</span>&gt;</span>27<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>while if you pass an integer to the method the user won&#8217;t be able to write a number of character greater than <em>maxLength</em> and  you will get a counter like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;textareaId_counter&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;length&quot;</span>&gt;</span>27<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sep&quot;</span>&gt;</span>/<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;maxLength&quot;</span>&gt;</span>500<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>This is the code. Save it in a text file named something like <em>jquery.textareaCharacterCounter.js</em> and include it in your page:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">setCounter</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>maxLength<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</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>
&nbsp;
    <span style="color: #003366; font-weight: bold;">var</span> jqthis <span style="color: #339933;">=</span> jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>jqthis.<span style="color: #000066; font-weight: bold;">is</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'textarea'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #003366; font-weight: bold;">var</span> counterId <span style="color: #339933;">=</span> jqthis.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;_counter&quot;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #003366; font-weight: bold;">var</span> htmlCounter <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;div id=&quot;'</span> <span style="color: #339933;">+</span> counterId <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;'</span><span style="color: #339933;">;</span>
      htmlCounter <span style="color: #339933;">+=</span> <span style="color: #3366CC;">'&lt;span class=&quot;length&quot;&gt;'</span> <span style="color: #339933;">+</span> jqthis.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/span&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>maxLength<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        htmlCounter <span style="color: #339933;">+=</span> <span style="color: #3366CC;">'&lt;span class=&quot;sep&quot;&gt;/&lt;/span&gt;'</span><span style="color: #339933;">;</span>
        htmlCounter <span style="color: #339933;">+=</span> <span style="color: #3366CC;">'&lt;span class=&quot;maxLength&quot;&gt;'</span> <span style="color: #339933;">+</span> maxLength <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/span&gt;'</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      htmlCounter <span style="color: #339933;">+=</span> <span style="color: #3366CC;">'&lt;/div&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
      jqthis.<span style="color: #660066;">after</span><span style="color: #009900;">&#40;</span>htmlCounter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span><span style="color: #339933;">+</span>counterId<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">width</span><span style="color: #009900;">&#40;</span>jqthis.<span style="color: #660066;">width</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #3366CC;">'text-align'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'right'</span><span style="color: #339933;">,</span>
        <span style="color: #3366CC;">'margin-top'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'10px'</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      jqthis.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;keyup&quot;</span><span style="color: #339933;">,</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>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> content <span style="color: #339933;">=</span> jqthis.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>maxLength <span style="color: #339933;">&amp;&amp;</span> content.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> maxLength<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
          content <span style="color: #339933;">=</span> content.<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>
          jqthis.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span> content <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span> <span style="color: #339933;">+</span> counterId <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &gt; span.length&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>content.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>        
&nbsp;
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Not applicable to element: &quot;</span> <span style="color: #339933;">+</span> jqthis<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>How to use it</h3>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Add a simple counter</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#textarea1'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">setCounter</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;">// Add a counter and set maximum length to 500 characters</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#textarea2'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">setCounter</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.melandri.net/2009/10/20/textarea-char-counter-a-jquery-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
