Uncaught TypeError: Cannot read property 'left' of undefined

Let's check datepicker _findPos function

$.datepicker._findPos = function (obj) {
        var position,
            inst = this._getInst(obj),
            isRTL = this._get(inst, "isRTL");

        while (obj && (obj.type === "hidden" || obj.nodeType !== 1 || $.expr.filters.visible(obj))) {
            obj = obj[isRTL ? "previousSibling" : "nextSibling"];
        }

        position = $(obj).offset();        

        /*because position of invisible element is null, js will break on next line*/
        return [position.left, position.top]; 
    };

If target obj of datepicker is invisible, it will use the closest sibling position which is not invisible

There are several solutions:

Solution 1

Because of LTR, you can exchange position of two element

<tr>
    <td> 
        <input type="hidden" class="end_date" />
        <small class="date_target">until <span>Dec. 31, 2013</span></small>
    </td>
</tr>

Solution 2

Add an visible element next to the hidden element, so datepicker will find the visible element position

<tr>
    <td>
        <small class="date_target">until <span>Dec. 31, 2013</span></small>
        <input type="hidden" class="end_date" /><span>&nbsp;</span>
    </td>
</tr>

Solution 3

Redefine _findPos function, so you can set position of calendar wherever you want

$.datepicker._findPos = function (obj) {
        var position,
            inst = this._getInst(obj),
            isRTL = this._get(inst, "isRTL");

        while (obj && (obj.type === "hidden" || obj.nodeType !== 1 || $.expr.filters.visible(obj))) {
            obj = obj[isRTL ? "previousSibling" : "nextSibling"];
        }

        position = $(obj).offset();
        // if element type isn't hidden, use show and hide to find offset
        if (!position) { position = $(obj).show().offset(); $(obj).hide();}
        // or set position manually
        if (!position) position = {left: 999, top:999};
        return [position.left, position.top]; 
    };

It's because the input field is not visible to the browser (because it's hidden).

Try this:

<input type="text" style="height: 0px; width:0px; border: 0px;" class="end_date" />

and you are fine. Of course, you can add the extra style attributes to the CSS class "end_date". A "display:none" will not help, because then the field is fully invisible again.

Example also in a JS Fiddle.

Tags:

Jquery Ui