0

Using Knockout JS 2.0 with jQuery 1.7.2 I am having an issue with live (on) events with objects created by my observableArray.

if I change the target of my function to a dom element that is on the page already it works, however when targeting an object that will be created it seems to ignore it. I've only just started looking at the on binding in jQuery so it's more than likely I've missed something.

Any help anybody can provide would be appreciated.

http://jsfiddle.net/kMsQL

Brett Smith
  • 2,992
  • 1
  • 20
  • 22

1 Answers1

1

To setup on for future elements it is like this:

http://jsfiddle.net/lucuma/yD3gL/1/

$('ul').on('mouseenter', 'div',   function() {    
        $(this).addClass('hover');
    });

$('ul').on('mouseleave', 'div',   function() {
        $(this).removeClass('hover');
    });

The old deprecated way using live also still works but isnt recommended:

http://jsfiddle.net/lucuma/kMsQL/26/

$('div').live( {
    mouseenter: function() {
        $(this).addClass('hover');
    },
    mouseleave: function() {
        $(this).removeClass('hover');
    }
});​
lucuma
  • 18,247
  • 4
  • 66
  • 91
  • No problem, sometimes it is hard to keep up with all the jQuery changes from version to version. – lucuma May 29 '12 at 03:53