<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: rails code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 06 Sep 2008 04:05:35 GMT</pubDate>
    <description>DZone Snippets: rails code</description>
    <item>
      <title>Javascript code to set Rails date_select to today.</title>
      <link>http://snippets.dzone.com/posts/show/3146</link>
      <description>If you are using date_select in rails here is a little bit of javascript so that at the side of the fields the user can click a link that will automatically set the fields to todays date.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Updated: Now works on rails 1.2, as date_select now finally has ids&lt;/b&gt; &lt;br /&gt;&lt;code&gt;&lt;br /&gt;# put is app_root/public/javascript/application.js&lt;br /&gt;&lt;br /&gt;function set_today(model, atrib)&lt;br /&gt;{&lt;br /&gt;    t3 = document.getElementById(model + '_' + atrib + '_3i');&lt;br /&gt;    var dt = new Date(); &lt;br /&gt;    t3.selectedIndex = dt.getDate();&lt;br /&gt;	&lt;br /&gt;    t2 = document.getElementById(model + '_' + atrib + '_2i')&lt;br /&gt;    t2.selectedIndex = dt.getMonth() + 1;&lt;br /&gt;    &lt;br /&gt;    t1 = document.getElementById(model + '_' + atrib + '_1i')&lt;br /&gt;&lt;br /&gt;    for (i = 0; i &lt; t1.length; i++)&lt;br /&gt;	       {&lt;br /&gt;	           if (t1.options[i].text == dt.getFullYear())&lt;br /&gt;	           {&lt;br /&gt;	               t1.selectedIndex = i;&lt;br /&gt;	          }&lt;br /&gt;    } &lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;To call this method say in a link use this in your rhtml&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;a href="javascript: set_today('model_name', 'column_name');"&gt;today?&lt;/a&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 13 Dec 2006 20:45:28 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3146</guid>
      <author>mrblack (Andy)</author>
    </item>
  </channel>
</rss>
