-1

I need to simulate mouse event on dynamically created object (menu item) it works on statically created menu, like this:

$("#Help").trigger("mouseenter");

but doesn't work on menu, that was created dynamically:

$("#wsnmotes").trigger("mouseenter");

I know, that it is necessary to create a 'delegate', but can't figure out the syntax. Please, help. Jan

John R
  • 2,741
  • 2
  • 13
  • 32
user3358452
  • 77
  • 1
  • 1
  • 5
  • Provide more context to your question. How do you bind mouseenter event? When do you try to trigger it? Etc... – A. Wolff Feb 23 '16 at 09:44
  • Can u post ur dynamic generated code and trigger event also? – Dhara Feb 23 '16 at 09:44
  • Possible duplicate of [Event binding on dynamically created elements?](http://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – twernt Feb 23 '16 at 20:12

1 Answers1

0

Can you once confirm if you are using like following

$( "body" ).on( "mouseenter", "#wsnmotes", function() {
  //do something
});

You should use on so dynamically added elements will be attached events as well.

As you are actually binding your event with parent, which exists always, it does work with dynamically added elements.

Ganesh Nemade
  • 1,504
  • 12
  • 17
  • Thanks everyone responding, – user3358452 Feb 23 '16 at 21:17
  • Thanks everyone responding, Your code works, but it is not what I need. My code is invoked like this: $.fn.IMclickhandler = function(data){ //alert('YOU clicked ME: ' + data.key); $('#patientboxsend').append("mote IP: "+ client_motes[0] + "\r"); // + "\r" $('#patientboxsend').animate({scrollTop:$('#patientboxsend')[0].scrollHeight - $('#patientboxsend').height()},100); $('#Help').trigger('mouseenter'); // works $("#wsnmotes").trigger('mouseenter'); // doesn't work }; – user3358452 Feb 23 '16 at 21:22
  • What I need to do is to do programmatically what the user would do by interacting with dropdown menu with mose (opening submenu) Once again it works on statically created menu items not on dynamically created ones. – user3358452 Feb 23 '16 at 21:25
  • code for dynamic menu (works) for (i=0; i ul').append( '
  •  mote IP: ' + motename + '' + '
  • ' ); } else{ //alert("already exist"); }; }; – user3358452 Feb 23 '16 at 21:28
  • Can anyone help, please? – user3358452 Feb 24 '16 at 21:20