<?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>Binary Doodles &#187; Javascript</title>
	<atom:link href="http://nithinbekal.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://nithinbekal.com</link>
	<description>Ruby on Rails, Web 2.0, Wordpress and more...</description>
	<lastBuildDate>Fri, 30 Jul 2010 17:49:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>JavaScript: How to create a simple countdown timer</title>
		<link>http://nithinbekal.com/2009/12/06/javascript-how-to-create-a-simple-countdown-timer/</link>
		<comments>http://nithinbekal.com/2009/12/06/javascript-how-to-create-a-simple-countdown-timer/#comments</comments>
		<pubDate>Sun, 06 Dec 2009 13:03:53 +0000</pubDate>
		<dc:creator>Nithin Bekal</dc:creator>
				<category><![CDATA[How-To]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://nithinbekal.com/?p=128</guid>
		<description><![CDATA[Create a simple countdown timer using javascript and HTML that counts down to an event. This example used in this tutorial counts down to the FIFA world cup 2010.]]></description>
			<content:encoded><![CDATA[<p>After yet another disappointing performance yesterday by the football (soccer, if you&#8217;re American) club I support, Liverpool, this was my tweet:</p>
<blockquote><p>I&#8217;ve already started counting days until FIFA world cup. Football season seem practically over for Liverpool. :(</p></blockquote>
<p>I actually did count the days, but being a web developer, I ended up writing javascript code to do the dirty work for me. ;-) I&#8217;ve never really done much with javascript date and time, so I&#8217;m not sure if there&#8217;s a better way to do this, but this works. Follow these steps to create a countdown timer for FIFA World Cup 2010. It&#8217;s easy to customize it for anything else.</p>
<p>First, we have to create a div with id as &#8220;worldcup_countdown_time&#8221; where you want to display the countdown timer. This div will be modified by our script to display the time remaining for the World Cup kick off.</p>
<pre>&lt;div id="worldcup_countdown_time"&gt; &lt;/div&gt;</pre>
<p>Now let&#8217;s write the script that will calculate the number of days, hours, seconds and minutes left for kick off. We&#8217;ll write each of the steps first and then put it all in a function called <code>updateWCTime()</code>.</p>
<p>First we have to calculate the time difference between the current time and the time of kickoff. Here, <code>now</code> is the current time, and <code>kickoff</code> is the kickoff time. We store the difference (which we get in milliseconds) in a variable called <code>diff</code>.</p>
<pre>
    now = new Date();
    kickoff = new Date.parse("June 11, 2010 11:30:00");
    diff = kickoff - now;
</pre>
<p>We find the time left till kickoff in terms of days, minutes, hours and seconds as shown below. We use the <code>Math.floor</code> function to get the absolute values in the variables. It returns the highest integer that&#8217;s less then the floating point value.</p>
<pre>
    days  = Math.floor( diff / (1000*60*60*24) );
    hours = Math.floor( diff / (1000*60*60) );
    mins  = Math.floor( diff / (1000*60) );
    secs  = Math.floor( diff / 1000 );
</pre>
<p>Now these variables will give us the exact number of hours, minutes and seconds left, but that&#8217;s not exactly what we want. If the remaining time were a day and 7 hours, we would have the value 31 (24+7) in the <code>minutes</code> variable. But what we want is the value 1 in the <code>day</code> variable, and the value 7 in the <code>hours</code>. Let&#8217;s create another set of variables to do that.</p>
<pre>
    dd = days;
    hh = hours - days  * 24;
    mm = mins  - hours * 60;
    ss = secs  - mins  * 60;
</pre>
<p>Now that the time has been calculated, you have to update the div you created at the beginning.</p>
<pre>
    document.getElementById("worldcup_countdown_time")
        .innerHTML =
            dd + ' days ' +
            hh + ' hours ' +
            mm + ' minutes ' +
            ss + ' seconds';
</pre>
<p><b>Putting it all together</b></p>
<p>Now, to make the countdown timer work, let&#8217;s put it all together.</p>
<pre>
function updateWCTime() {
	now      = new Date();
	kickoff  = new Date.parse("June 11, 2010 11:30:00");
	diff = kickoff - now;

	days  = Math.floor( diff / (1000*60*60*24) );
	hours = Math.floor( diff / (1000*60*60) );
	mins  = Math.floor( diff / (1000*60) );
	secs  = Math.floor( diff / 1000 );

	dd = days;
	hh = hours - days  * 24;
	mm = mins  - hours * 60;
	ss = secs  - mins  * 60;

        document.getElementById("worldcup_countdown_time")
            .innerHTML =
                dd + ' days  +
                hh + ' hours ' +
                mm + ' minutes ' +
                ss + ' seconds';
}
setInterval('updateWCTime()', 1000 );
</pre>
<p>The last line in the above code creates a timer that calls the <code>updateWCTime()</code> every 1000ms to refresh the timer every second.</p>
<p>Put this script in a .js file and include it in your HTML page to turn the dv with id &#8220;worldcup_countdown_time&#8221; into a countdown timer that counts down to the world cup kickoff time. There you go, you have a working countdown timer ready now with something like the following diplayed in the div:</p>
<pre>186 days 17 hours 12 minutes 14 seconds</pre>
<p>To count down to some other event, you can easily change the value in the <code>kickoff</code> variable to the date that you want to use.</p>
<p>Update. I&#8217;ve put up <a href="http://gist.github.com/299417">the code on gihub</a> where you can also <a href="http://gist.github.com/gists/299417/download">download it as tar.gz</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://nithinbekal.com/2009/12/06/javascript-how-to-create-a-simple-countdown-timer/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>How to create a simple datepicker using jQuery</title>
		<link>http://nithinbekal.com/2009/08/09/how-to-create-a-simple-datepicker-using-jquery/</link>
		<comments>http://nithinbekal.com/2009/08/09/how-to-create-a-simple-datepicker-using-jquery/#comments</comments>
		<pubDate>Sun, 09 Aug 2009 14:33:00 +0000</pubDate>
		<dc:creator>Nithin Bekal</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Datepicker]]></category>

		<guid isPermaLink="false">http://nithinbekal.com/2009/08/09/how-to-create-a-simple-datepicker-using-jquery/</guid>
		<description><![CDATA[jQuery allows you to easily create a datepicker and customize it according to your requirements. Here&#8217;s a quick look at how you can get started with using the jQuery datepicker.
The jQuery website has a nice application called Themeroller that lets you customize the interface for jQuery UI widgets. There are also plenty of themes available [...]]]></description>
			<content:encoded><![CDATA[<p>jQuery allows you to easily create a datepicker and customize it according to your requirements. Here&#8217;s a quick look at how you can get started with using the jQuery datepicker.</p>
<p>The jQuery website has a nice application called <a href="http://jqueryui.com/themeroller/">Themeroller</a> that lets you customize the interface for jQuery UI widgets. There are also plenty of themes available in the gallery that you can download and use directly. Copy the stylesheet, images, the jQuery file and the jQuery-UI files into the appropriate directories and link them in the header of your HTML file.</p>
<p>Now let&#8217;s create a simple form that contains the text field that you want to convert into a datepicker.</p>
<pre>&lt;form action="index" method="get"&gt;
  &lt;input id="date-pick" name="date-pick" type="text" value="" /&gt;
  &lt;input id="date-submit" name="date-submit" type="submit" value="Go!" /&gt;
&lt;/form&gt;</pre>
<p>To convert the input field with the id <code>date-pick</code> into a datepicker, write the following jQuery code in the head section of your HTML.</p>
<pre>$(function(){
  $("#date-pick").datepicker();
});</pre>
<div class="separator" style="float: right; width: 250px;"><a style="margin-left: 1em; margin-right: 1em;" href="http://2.bp.blogspot.com/_SPbUIiu4sfA/Sn7XP4QC2SI/AAAAAAAACAg/Q9rZReomeAE/s1600-h/datepicker.png"><img src="http://2.bp.blogspot.com/_SPbUIiu4sfA/Sn7XP4QC2SI/AAAAAAAACAg/Q9rZReomeAE/s320/datepicker.png" border="0" alt="" /></a></div>
<p>That&#8217;s it! The input field that you created now works as a popup datepicker. A calendar showing the current month appears whenever you focus on the <code>#date-pick</code> field and the date you click on appears as text in the field.</p>
<p>There are tons of options to customize the datepicker, such as changing the date format sent to the server or using a different language for the calendar. You can explore these options in the <a href="http://docs.jquery.com/UI/Datepicker">jQuery UI documentation for the datepicker</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://nithinbekal.com/2009/08/09/how-to-create-a-simple-datepicker-using-jquery/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>jQuery: Avoiding conflict with other libraries using jQuery.noconflict()</title>
		<link>http://nithinbekal.com/2009/08/01/jquery-avoiding-conflict-with-other-libraries-using-jquery-noconflict/</link>
		<comments>http://nithinbekal.com/2009/08/01/jquery-avoiding-conflict-with-other-libraries-using-jquery-noconflict/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 18:47:00 +0000</pubDate>
		<dc:creator>Nithin Bekal</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://nithinbekal.com/2009/08/01/jquery-avoiding-conflict-with-other-libraries-using-jquery-noconflict/</guid>
		<description><![CDATA[Recently, when using jQuery in my Ruby on Rails project, the jQuery calendar plugin I was using suddenly refused to work in one of the views. The plugin was working fine everywhere else, but it just wouldn&#8217;t work here.
The problem was that there was a conflict with a rails prototype helper (observe_field) that I was [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, when using jQuery in my Ruby on Rails project, the jQuery calendar plugin I was using suddenly refused to work in one of the views. The plugin was working fine everywhere else, but it just wouldn&#8217;t work here.</p>
<p>The problem was that there was a conflict with a rails prototype helper <code>(observe_field)</code> that I was using in the view. Using jQuery along with other javascript frameworks such as prototype or mootools causes a namespace clash as they all use the <code>$</code> alias.</p>
<p>jQuery provides a <code>noconflict</code> method to avoid this clash, which lets you rename the jQuery function (<code>$</code>) as something else.</p>
<p>In the code below, the <code>noconflict</code> method is called to replace the $ alias with the function name <code>jQuery</code> and release <code>$</code> to other frameworks.</p>
<pre>
&lt;script src="jquery.js"&gt;&amp;lt/script&gt;
&lt;script&gt;
  jQuery.noConflict();
  jQuery(document).ready(function(){
    // your code goes here.
  });
&lt;/script&gt;
</pre>
<p>However, you can also use some other name instead of jquery by assigning <code>jQuery.noconflict()</code> to another variable. In the code below, I have use <code>$jq</code> as the new alias for the jQuery function. Now you must use this new alias where you would otherwise have used the $ shorthand.</p>
<pre>
&lt;script src="jquery.js"&gt;&amp;lt/script&gt;&lt;script&gt;
  var $jq = jQuery.noConflict();
  $jq(document).ready(function(){
    // your code goes here.
  });&lt;/script&gt;
</pre>
<p>Using this form of the <code>noconflict</code> method allows you to avoid namespace conflicts while at the same time giving you the ability to use a short alias (<code>$jq</code> above) for jQuery.</p>
<p>If you want to globally disable the <code>$</code> alias for jQuery, call the <code>noconflict</code> method from the last line of the jQuery file.</p>
<p>Make sure you call this function before using the conflicting library, otherwise it may still clash. However, you may include the other library without causing any conflict.</p>
]]></content:encoded>
			<wfw:commentRss>http://nithinbekal.com/2009/08/01/jquery-avoiding-conflict-with-other-libraries-using-jquery-noconflict/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
