Solved

jqery ui datepicker :- passing variables

Posted on 2011-03-01
4
786 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

822 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