Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

jqery ui datepicker :- passing variables

Posted on 2011-03-01
4
Medium Priority
?
790 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 1500 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

Google Certified Professional - Cloud Architect

This course (1 of 3) is designed to help students who are interested in Google Cloud Platform (GCP) to become familiar with the platform, navigate the console and learn its capabilities. It will also prepare students for the Google Cloud Architect certification exam.

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
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…

721 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