0

When I use the following, I see all divs static and dynamic one by one including the dynamically added div #xyz

jQuery('div').livequery(function() { alert($(this).attr("id") + " div added") })

but when I use

jQuery('#xyz').livequery(function() { alert($(this).attr("id") + " div added") })

I get nothing. However - if xyz was in static html, the above works.

Eventually I want to be able to click a button programmatically when added dynamically.

Any help is appreciated.

Pointy
  • 405,095
  • 59
  • 585
  • 614
prat
  • 607
  • 2
  • 8
  • 22
  • Solution. Don't use livequery. It's not maintained/supported anymore and using it is just being lazy. – Raynos Feb 09 '11 at 16:59
  • whats your solution to click a button programmatically when its added dynamically. – prat Feb 09 '11 at 17:06

1 Answers1

1

Just use jQuery.live. This will attach event handler to the elements that match the selector now and in future.

Example

$('a.foo').live('click', function() {
    alert('Clicked!');
});

I don't know if you can catch the event, when new elements are added to the DOM, but in general you want to apply behaviour (when an event occurs) to them anyway.

Reiner Gerecke
  • 11,936
  • 1
  • 49
  • 41