Solved

jqery ui datepicker :- passing variables

Posted on 2011-03-01
4
788 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
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…

752 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