I'm shocked that:
a) no one here has mentioned a programmatic solution to setting the datepicker z-Index
b) there is no option for the jQuery DatePicker to pass a z-Index when binding it to an element.
I am using a js method to calculate the highest index. We default to checking only divs, because these are the likely elements to get a z-index value and is understandably quicker than parsing every element on the page.
/* Get the highest zindex for elements on the page
*/
function getHighestZIndex(element){
var index_highest = 0;
var index_current;
if(element == undefined){
element = 'div';
}
$(element).each(function(){
index_current = parseInt($(this).css("z-index"), 10);
if(index_current > index_highest) {
index_highest = index_current;
}
});
return index_highest;
}
Because we have to use javascript to bind the datepicker to an element, i.e. $('#some_element').datepicker(), at that time we set the target element's style so that datepicker will pick up the z-index from it. Thanks to Ronye Vernaes (in his answer on this page) for pointing out that datepicker() will pick up the target element's z-Index if it has one and is set to position: relative:
$(this.target).datepicker();
var zindex = e.getHighestZIndex();
zindex++;
$(this.target).css('position','relative');
$(this.target).css('z-index',zindex);
this.target is the input element we are making into a datepicker field.
At binding time, we get the highest z-index on the page and make the element's z-index one higher.
When datepicker icon is clicked, it will pick up that z-index from the input element, because, as Ronye Vernaes mentioned, of the style position: relative.
In this solution, we don't try and guess what the highest z-Index value is going to be. We actually GET IT.