Cannot set the minDate of a dynamically added element

Citrus

I have a webpage which has a repeating partial view which is being bound to a Knockout template. Within that view, I have a datepicker. That view can be added any number of times on a button click. The selection of a date in section 1, sets the minDate of the datepicker in section 2, and so on. Each section has its own Knockout view model, which has a minDate observable. Attached to this observable, I have a custom binding with an update handler - I use this to update the minDate of an element when a section is added or updated.

I have the code working up to the point where I can update the value of a datepicker in any section, and the following section will have its minDate correctly updated. Here is the code for my custom binding:

ko.bindingHandlers.minDate = {
    update: function (element, valueAccessor, allBindings, viewModel) {
        var value = valueAccessor();
        var valueUnwrapped = ko.unwrap(value);
        var oneDay = 1000 * 60 * 60 * 24;
        var diff = Math.ceil((new Date(valueUnwrapped) - new Date()) / oneDay);
        $(element).datepicker('option', 'minDate', diff + 1);
    }
};

My problem is that if I have existing sections with dates selected and I click to add a new a new section, I can see the minDate of my newly added view model is correctly set, but that minDate is not being set on my newly added datepicker element.

If I step through my script code, the only difference I can see is that even thought at the point I break execution, my partial view has been rendered and my $(element) is indeed an object but it does not have an ID attribute. This is the only difference I can see between my working and non-working code.

Can anyone see this as a possible cause for my problem with setting the minDate on a dynamically added element?

Citrus

In my code where I add dynamic sections, I originally had:

var newSection = new Components.Booking.TripViewModel();
var prevTrip = last(self.model.trips());
newSection.model.minDate(prevTrip.model.departureDate());
self.model.trips.push(newTrip);

When I realised that when I added a new view model to my section collection, that is the point at which the template renders my partial view and applies the bindings etc., which is also when the element was being assigned an ID. I simply needed to reverse the order of the last two lines, to ensure that my minDate observable is updated after the element has been bound up, and therefore when my update handler within the custom binding fires after the element has been fully rendered.

self.model.trips.push(newTrip);
newSection.model.minDate(prevTrip.model.departureDate());    

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cannot remove a dynamically added element

From Dev

Dynamically added element will not delete

From Dev

Add class to dynamically added element

From Dev

JQuery Selector on Dynamically Added Element

From Dev

find index of dynamically added element

From Dev

why element is not added in dynamically in angular

From Dev

jQuery click on a dynamically added element

From Dev

Check if dynamically added element is ready

From Dev

How can I set the height of an element which has been dynamically added to the page

From Dev

Cannot get outerHTML/value of a dynamically added textarea

From Dev

how to use jquery .data with dynamically added element

From Dev

change name of dynamically added select option element

From Dev

Bootstrap Select - reinitialize on dynamically added element

From Dev

How to remove an element that is added dynamically in Javascript

From Dev

How to change the css of a dynamically added element?

From Dev

Jquery changes on element which is added dynamically is not working

From Dev

jQuery on blur / focusout not firing on dynamically added element

From Dev

Remove a dynamically added element without using on()

From Dev

jQuery UI droppable on dynamically added element?

From Dev

jQuery select element with class added dynamically

From Dev

change name of dynamically added select option element

From Dev

Get calculated width of dynamically added UI element

From Dev

Jquery selector not behaving dynamically on added dom element

From Dev

How to slice text dynamically added to an element?

From Dev

jQuery on blur / focusout not firing on dynamically added element

From Dev

Click not recognized on element with dynamically added class

From Dev

jQuery UI droppable on dynamically added element?

From Dev

Dynamically added element function call not working

From Dev

JS: Target and remove dynamically added element

Related Related

  1. 1

    Cannot remove a dynamically added element

  2. 2

    Dynamically added element will not delete

  3. 3

    Add class to dynamically added element

  4. 4

    JQuery Selector on Dynamically Added Element

  5. 5

    find index of dynamically added element

  6. 6

    why element is not added in dynamically in angular

  7. 7

    jQuery click on a dynamically added element

  8. 8

    Check if dynamically added element is ready

  9. 9

    How can I set the height of an element which has been dynamically added to the page

  10. 10

    Cannot get outerHTML/value of a dynamically added textarea

  11. 11

    how to use jquery .data with dynamically added element

  12. 12

    change name of dynamically added select option element

  13. 13

    Bootstrap Select - reinitialize on dynamically added element

  14. 14

    How to remove an element that is added dynamically in Javascript

  15. 15

    How to change the css of a dynamically added element?

  16. 16

    Jquery changes on element which is added dynamically is not working

  17. 17

    jQuery on blur / focusout not firing on dynamically added element

  18. 18

    Remove a dynamically added element without using on()

  19. 19

    jQuery UI droppable on dynamically added element?

  20. 20

    jQuery select element with class added dynamically

  21. 21

    change name of dynamically added select option element

  22. 22

    Get calculated width of dynamically added UI element

  23. 23

    Jquery selector not behaving dynamically on added dom element

  24. 24

    How to slice text dynamically added to an element?

  25. 25

    jQuery on blur / focusout not firing on dynamically added element

  26. 26

    Click not recognized on element with dynamically added class

  27. 27

    jQuery UI droppable on dynamically added element?

  28. 28

    Dynamically added element function call not working

  29. 29

    JS: Target and remove dynamically added element

HotTag

Archive