Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Andy www.mrblack.co.uk/blog

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Javascript code to set Rails date_select to today.

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.

Updated: Now works on rails 1.2, as date_select now finally has ids
# put is app_root/public/javascript/application.js

function set_today(model, atrib)
{
    t3 = document.getElementById(model + '_' + atrib + '_3i');
    var dt = new Date(); 
    t3.selectedIndex = dt.getDate();
	
    t2 = document.getElementById(model + '_' + atrib + '_2i')
    t2.selectedIndex = dt.getMonth() + 1;
    
    t1 = document.getElementById(model + '_' + atrib + '_1i')

    for (i = 0; i < t1.length; i++)
	       {
	           if (t1.options[i].text == dt.getFullYear())
	           {
	               t1.selectedIndex = i;
	          }
    } 
}





To call this method say in a link use this in your rhtml

<a href="javascript: set_today('model_name', 'column_name');">today?</a>
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS