Knockout click binding after bubbling

MPavlak

Is there a way to cause the click to run after the event has bubbled? In the below scenario, clicking the span triggers removeChildNode function which removes the span from dom. this prevents the span's click from triggering a click on the 'div[data-identifier]' node. I'd like the event to bubble up and then the function to execute.

<div data-identifier>
  <div data-bind="click: removeChildNode">
    <span>Click Me!</span>
  </div>
</div>

removeChildNode = function() { $(arguments[1].target).children()[0].remove(); }

$('body').on("click", 'span', function(event) {
  // use the span to navigate around and do something interesting
  $(event.target).closest('div[data-identifier]').click();
});
Roy J

If you're going to use Knockout, you need to let it control the DOM, and you manipulate your viewmodel. You should have some viewmodel entity that represents the removable child, and your removeChildNode function would remove it (or set it into a state that indicates it is removed). As a general rule, jQuery selectors indicate you have failed to model something.

There is no point in having two ways to set up click events. If you are going to use a jQuery click event in one place, you should use jQuery for all your click events and not use Knockout.

That said, by default, Knockout does allow events to bubble. You could put a setTimeout around the node remover to give the DOM time to propagate events before you remove the node, if that's what's keeping your outer click from firing. You will have fewer problems of this sort if you leave the DOM entirely to Knockout.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Conditional 'click' binding with Knockout

From Dev

Knockout if, click and visible binding

From Dev

Why is a click on a nested element not bubbling up to it's parent with the click binding?

From Dev

Knockout click binding not working when hiding the element

From Dev

Knockout click binding with javascript confirm

From Dev

Toggle a bootstrap dropdown with Knockout click binding

From Dev

Knockout.js click binding strange behavior

From Dev

Multiple click binding on element only executes one in knockout.js?

From Dev

Knockout click binding strange behavior

From Dev

Knockout foreach binding calling click event while iteration

From Dev

Knockout click binding how to get Array of the "current item"?

From Dev

Knockout: click binding to add/remove item in hierarchy

From Dev

How to send parameter to a click binding knockout

From Dev

Knockout JS click binding

From Dev

Knockout if binding

From Dev

Knockout "with" binding

From Dev

Update view model through ajax request through click binding in knockout

From Dev

knockout js click binding not working

From Dev

Knockout.js click binding strange behavior

From Dev

Multiple click binding on element only executes one in knockout.js?

From Dev

knockout radio button click binding not working

From Dev

Dynamic binding in Knockout click event

From Dev

Binding click in Bootstrap's popover using Knockout.js

From Dev

Knockout JS click binding

From Dev

Is it necessary to unbind/off JQuery click event inside Knockout custom binding?

From Dev

Knockout - Click Binding Behaviour

From Dev

What is Knockout custom binding "after" variable?

From Dev

Binding breaks after button click

From Dev

checkbox knockout click binding not working properly

Related Related

  1. 1

    Conditional 'click' binding with Knockout

  2. 2

    Knockout if, click and visible binding

  3. 3

    Why is a click on a nested element not bubbling up to it's parent with the click binding?

  4. 4

    Knockout click binding not working when hiding the element

  5. 5

    Knockout click binding with javascript confirm

  6. 6

    Toggle a bootstrap dropdown with Knockout click binding

  7. 7

    Knockout.js click binding strange behavior

  8. 8

    Multiple click binding on element only executes one in knockout.js?

  9. 9

    Knockout click binding strange behavior

  10. 10

    Knockout foreach binding calling click event while iteration

  11. 11

    Knockout click binding how to get Array of the "current item"?

  12. 12

    Knockout: click binding to add/remove item in hierarchy

  13. 13

    How to send parameter to a click binding knockout

  14. 14

    Knockout JS click binding

  15. 15

    Knockout if binding

  16. 16

    Knockout "with" binding

  17. 17

    Update view model through ajax request through click binding in knockout

  18. 18

    knockout js click binding not working

  19. 19

    Knockout.js click binding strange behavior

  20. 20

    Multiple click binding on element only executes one in knockout.js?

  21. 21

    knockout radio button click binding not working

  22. 22

    Dynamic binding in Knockout click event

  23. 23

    Binding click in Bootstrap's popover using Knockout.js

  24. 24

    Knockout JS click binding

  25. 25

    Is it necessary to unbind/off JQuery click event inside Knockout custom binding?

  26. 26

    Knockout - Click Binding Behaviour

  27. 27

    What is Knockout custom binding "after" variable?

  28. 28

    Binding breaks after button click

  29. 29

    checkbox knockout click binding not working properly

HotTag

Archive