<?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; Javascript</title>
	<atom:link href="http://www.melandri.net/tag/javascript/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>Today links</title>
		<link>http://www.melandri.net/2009/11/19/today-links-4/</link>
		<comments>http://www.melandri.net/2009/11/19/today-links-4/#comments</comments>
		<pubDate>Thu, 19 Nov 2009 08:36:13 +0000</pubDate>
		<dc:creator>amelandri</dc:creator>
				<category><![CDATA[Links]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://www.melandri.net/?p=732</guid>
		<description><![CDATA[Good karma: an in-depth review of Ubuntu 9.10 Ars review Simply Buttons v2 Javascript/CSS stylish buttons How to wire your house for Ethernet networking from Instructables 10 Easy jQuery Tricks for Designers]]></description>
			<content:encoded><![CDATA[<ul>
<li><a href="http://arstechnica.com/open-source/reviews/2009/11/good-karma-ars-reviews-ubuntu-910.ars">Good karma: an in-depth review of Ubuntu 9.10</a> Ars review</li>
<li><a href="http://www.p51labs.com/simply-buttons-v2/">Simply Buttons v2</a> Javascript/CSS stylish buttons</li>
<li><a href="http://www.instructables.com/id/How_to_Wire_Your_House_With_Cat_5_or_6_For_Ether/">How to wire your house for Ethernet networking</a> from Instructables</li>
<li><a href="http://sixrevisions.com/javascript/10-easy-jquery-tricks-for-designers/">10 Easy jQuery Tricks for Designers</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.melandri.net/2009/11/19/today-links-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
		<item>
		<title>Check date validity with Javascript</title>
		<link>http://www.melandri.net/2009/09/02/check-date-validity-with-javascript/</link>
		<comments>http://www.melandri.net/2009/09/02/check-date-validity-with-javascript/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 20:16:29 +0000</pubDate>
		<dc:creator>amelandri</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://www.melandri.net/?p=537</guid>
		<description><![CDATA[A simple JavaScript function to check date validity function checkDateValidity&#40;day, month, year&#41;&#123; &#160; month = month - 1; var message = ''; &#160; var date = new Date&#40;year,month,day&#41;; &#160; if &#40;year != date.getFullYear&#40;&#41;&#41; message = 'Year not valid'; &#160; if &#40;month != date.getMonth&#40;&#41;&#41; message = 'Month not valid'; &#160; if &#40;day != date.getDate&#40;&#41;&#41; message = [...]]]></description>
			<content:encoded><![CDATA[<p>A simple JavaScript function to check date validity</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> checkDateValidity<span style="color: #009900;">&#40;</span>day<span style="color: #339933;">,</span> month<span style="color: #339933;">,</span> year<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
  month <span style="color: #339933;">=</span> month <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> message <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> date <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span>year<span style="color: #339933;">,</span>month<span style="color: #339933;">,</span>day<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>year <span style="color: #339933;">!=</span> date.<span style="color: #660066;">getFullYear</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    message <span style="color: #339933;">=</span> <span style="color: #3366CC;">'Year not valid'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>month <span style="color: #339933;">!=</span> date.<span style="color: #660066;">getMonth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    message <span style="color: #339933;">=</span> <span style="color: #3366CC;">'Month not valid'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>day <span style="color: #339933;">!=</span> date.<span style="color: #660066;">getDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    message <span style="color: #339933;">=</span> <span style="color: #3366CC;">'Day not valid'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">return</span> message<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.melandri.net/2009/09/02/check-date-validity-with-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Javascript getElementById() shortcut</title>
		<link>http://www.melandri.net/2009/08/03/javascript-getelementbyid-shortcut/</link>
		<comments>http://www.melandri.net/2009/08/03/javascript-getelementbyid-shortcut/#comments</comments>
		<pubDate>Mon, 03 Aug 2009 21:28:41 +0000</pubDate>
		<dc:creator>amelandri</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[shortcut]]></category>

		<guid isPermaLink="false">http://www.melandri.net/?p=113</guid>
		<description><![CDATA[Do you love the wonderful javascript dollar shortcut $('myId') but cannot use jQuery or Prototype? Don&#8217;t worry, you can define your custom shortcut for the too long document.getElementById() function: function $&#40;id&#41; &#123; return document.getElementById&#40;id&#41;; &#125; Now you can sobstitute this: var divContent = document.getElementById&#40;'myId'&#41;.innerHTML; with this: var divContent = $&#40;'myId'&#41;.innerHTML; Obviously you don&#8217;t get all [...]]]></description>
			<content:encoded><![CDATA[<p>Do you love the wonderful javascript dollar shortcut <code>$('myId')</code> but cannot use <a href="http://www.jquery.com">jQuery</a> or <a href="http://www.prototypejs.com">Prototype</a>? Don&#8217;t worry, you can define your custom shortcut for the too long <code>document.getElementById()</code> function:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> $<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now you can sobstitute this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> divContent <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myId'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span><span style="color: #339933;">;</span></pre></div></div>

<p>with this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> divContent <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myId'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span><span style="color: #339933;">;</span></pre></div></div>

<p>Obviously you don&#8217;t get all the bells and whistles that you would get using jQuery, but it&#8217;s a start.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.melandri.net/2009/08/03/javascript-getelementbyid-shortcut/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get location coordinates using Google Maps</title>
		<link>http://www.melandri.net/2009/07/03/get-location-coordinates-using-google-maps/</link>
		<comments>http://www.melandri.net/2009/07/03/get-location-coordinates-using-google-maps/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 09:17:49 +0000</pubDate>
		<dc:creator>amelandri</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[coordinates]]></category>
		<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[location]]></category>

		<guid isPermaLink="false">http://www.melandri.net/?p=120</guid>
		<description><![CDATA[This is a simple tutorial on finding location&#8217;s coordinates using Google Maps APIs. First of all you need to signup for a Google Maps API key, otherwise your script will not work. When you are done, start building a simple form with three fields: one for the location and the others for coordinates display. &#60;form [...]]]></description>
			<content:encoded><![CDATA[<p>This is a simple tutorial on finding location&#8217;s coordinates using <a href="http://maps.google.com" title="Google Maps">Google Maps</a> <a href="http://code.google.com/apis/maps/" title="Google Maps APIs">APIs</a>.</p>
<p>First of all you need to <a href="http://code.google.com/apis/maps/signup.html">signup</a> for a Google Maps API key, otherwise your script will not work.</p>
<p>When you are done, start building a simple form with three fields: one for the location and the others for coordinates display.</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;">form</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;myForm&quot;</span>&gt;</span>
  Address:<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;address&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;button&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Get coordinates&quot;</span> <span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;getCoordinates()&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
  Latitude:<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;lat&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
  Longitude:<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;lng&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></div></div>

<p>Now, we need to define the <code>getCoordinates()</code> function: this function must read the value of the <code>address</code> field, check if it&#8217;s valid and get its coordinates. To get coordinates we&#8217;ll use the <code>GClientGeocoder</code> <a href="http://code.google.com/apis/maps/documentation/reference.html#GClientGeocoder" title="GClientGeocoder documentation">class</a> and its method <code>getLatLng(address:String, callback:function)</code>:</p>
<blockquote><p>Sends a request to Google servers to geocode the specified address. If the address was successfully located, the user-specified callback function is invoked with a GLatLng  point. Otherwise, the callback function is given a null point. In case of ambiguous addresses, only the point for the best match is passed to the callback function. </p></blockquote>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> geocoder <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GClientGeocoder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> getCoordinates<span style="color: #009900;">&#40;</span>address<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> address <span style="color: #339933;">=</span> document.<span style="color: #660066;">myForm</span>.<span style="color: #660066;">address</span>.<span style="color: #660066;">value</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>address <span style="color: #339933;">!=</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    geocoder.<span style="color: #660066;">getLatLng</span><span style="color: #009900;">&#40;</span>address<span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>point<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>point <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        document.<span style="color: #660066;">myForm</span>.<span style="color: #660066;">lat</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">=</span> point.<span style="color: #660066;">lat</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        document.<span style="color: #660066;">myForm</span>.<span style="color: #660066;">lng</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">=</span> point.<span style="color: #660066;">lng</span><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: #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;Location not found&quot;</span><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: #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;Please insert a location&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And that&#8217;s all! Hit the more link to see the full page code.</p>
<p><span id="more-120"></span></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;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">&lt;!-- Visit http://code.google.com/apis/maps/signup.html for your custom url --&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;custom_api_url&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
&nbsp;
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">lang</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Javascript&quot;</span>&gt;</span>
&nbsp;
    var geocoder = new GClientGeocoder();
&nbsp;
    function getCoordinates(address){
      var address = document.myForm.address.value;
&nbsp;
      if (address != ''){
        geocoder.getLatLng(address,function(point){
          if (point != null){
            document.myForm.lat.value = point.lat();
            document.myForm.lng.value = point.lng();
          }
          else{
            alert(&quot;Location not found&quot;);
          }
        })
      }
      else{
        alert(&quot;Please insert a location&quot;);
      }
    }
&nbsp;
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
&nbsp;
  <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;map&quot;</span>&gt;</span>
&nbsp;
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;myForm&quot;</span>&gt;</span>
&nbsp;
      Location:<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;address&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span> 
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;button&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Coordinates&quot;</span> <span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;getCoordinates()&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
      Latitude:<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;lat&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
      Longitude:<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;lng&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
&nbsp;
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
&nbsp;
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.melandri.net/2009/07/03/get-location-coordinates-using-google-maps/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
