0

I am trying to add a datepicker via an Angularjs Directive while passing the element as shown below and it's not working, but if I specify an element id, it works fine:

HTML:

<input id="startdate" type="text" ng-model="i.itinerary.DapartDate" date-from />

app.js:

Not Working:

travelApp.directive('dateFrom', function () {
    return function (scope, element, attrs) {

        element.datepicker({ minDate: currentTime });
        element.change(function () {
            currentTime = element.val();
        });

    }
})

WORKING

travelApp.directive('dateFrom', function () {
    return function (scope, element, attrs) {

        $("startdate").datepicker({ minDate: currentTime });
        $("startdate").change(function () {
            currentTime = $("#startdate").val();
        });

    }
})

Unfortunately I can not use controls ids, because I have an array of start and end dates in my itinerary list, so I need to bind this function to every newly created date element in itinerary array. Please advise.

1 Answer 1

1

In your non-working example you try to call .datepicker on the element itself, you need to make it a jQuery object first!

$(element).datepicker({ minDate: currentTime });
$(element).change(function () {
    currentTime = element.val();
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.