Solved

jqery ui datepicker :- passing variables

Posted on 2011-03-01
4
784 Views
Last Modified: 2012-05-11
Having problems with two datepickers one of which the year dropdown caries the year from the other as it's minimum for year dropdown value.

The following code is executed, the onclose functions are called successfully but the global variable "ch_from_year" is not being altered by the onclose function although it is being used successfully within that function (i.e. it sets the right date for that datepicker but it does not seem to carry the value to the gloabal variable.)
var ch_from_year = '1940';
$(function() {
	$('#ch_from_date').datepicker({
		showOn: 'button',
		buttonImage: '../css/images/calendar.gif',
		buttonImageOnly: true,
		changeMonth: true,
		changeYear: true,
		minDate: new Date(1929, 1 - 1, 1),
		beforeShowDay: hideDays,
	 	yearRange: '1929:2010',
	 		dateFormat: 'mm-yy',
	        onClose: function(dateText, inst) { 
	            var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
	            ch_from_year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
	            $(this).datepicker('setDate', new Date(ch_from_year, month, 1));
	        		        }
    	 	
	});
	
	});

$(function() {
	$('#ch_to_date').datepicker({
		showOn: 'button',
		buttonImage: '../css/images/calendar.gif',
		buttonImageOnly: true,
		changeMonth: true,
		changeYear: true,
		minDate: new Date(ch_from_year, 1 - 1, 1),
		beforeShowDay: hideDays,
	 	yearRange: '1929:2010',
	 		dateFormat: 'mm-yy',
	        onClose: function(dateText, inst) { 
	            var to_month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
	            var to_year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
	            $(this).datepicker('setDate', new Date(to_year, to_month, 1));
	        }
			 	
	});

	});

Open in new window


Advice appreciated.

0
Comment
Question by:bjh497
  • 2
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
mhmr earned 500 total points
ID: 35010997
Hi  bjh497
I found two problems and i updated your code above:
var ch_from_year = '1940';
$(function() {
	$('#ch_from_date').datepicker({
		showOn: 'button',
		buttonImage: '../css/images/calendar.gif',
		buttonImageOnly: true,
		changeMonth: true,
		changeYear: true,
		minDate: new Date(1929, 1 - 1, 1),
		beforeShowDay: hideDays,
	 	yearRange: '1929:2010',
	 		dateFormat: 'mm-yy',
	        onClose: function(dateText, inst) { 
	            var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
	            window.ch_from_year = $("#ui-datepicker-div .ui-datepicker-year :selected").val(); //<==  add "window." befor th ch_from_year  to use the window scope 
				$( "#ch_to_date" ).datepicker( "option", "minDate", new Date(window.ch_from_year, 1 - 1, 1) ); // <==  Add this line to set the minDate option dynamically
	            $(this).datepicker('setDate', new Date(window.ch_from_year, month, 1)); //<==  also here add "window." befor th ch_from_year  to use the window scope 
	        		        }
    	 	
	});
	
	});

$(function() {
	$('#ch_to_date').datepicker({
		showOn: 'button',
		buttonImage: '../css/images/calendar.gif',
		buttonImageOnly: true,
		changeMonth: true,
		changeYear: true,
		minDate: new Date(ch_from_year, 1 - 1, 1),
		beforeShowDay: hideDays,
	 	yearRange: '1929:2010',
	 		dateFormat: 'mm-yy',
	        onClose: function(dateText, inst) { 
	            var to_month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
	            var to_year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
	            $(this).datepicker('setDate', new Date(to_year, to_month, 1));
	        }
			 	
	});

	});
			

Open in new window


I hope this should do it

regards.
0
 
LVL 2

Expert Comment

by:mhmr
ID: 35026907
the changes is on lines "15" "16" "17"
just to be clear


0
 

Author Comment

by:bjh497
ID: 35027739
sorry we had already fixed it.
0
 

Author Closing Comment

by:bjh497
ID: 35027747
we solved it internally but htis solution was nearly their.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now