jQuery: The Write Less, Do More JavaScript Library

Dynamic img not getting events

View: New views
3 Messages — Rating Filter:   Alert me  

Dynamic img not getting events

by Jose Hidalgo-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi all,

I have a very simple html structure and the idea is to create and use
DIVs or elements I do dynamically, so there is one div which I use to
do something like:

$("#main").html("<div id='img-lisg'><img id='img1' .....></div>");

That works just fine, the problem is when I try to use events on the
elements I've created dynamically, those just don't work. There is
something like this:
   ("#dynamicDiv").click(function(){
               alert('Testing');
   });

It never works,...   how should I create the handlers for the DIVs I
create dynamically ?

Thanks



Re: Dynamic img not getting events

by JT-28 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

To bind events, such as 'click', to elements that are added to the
page dynamically, as with AJAX, you can use a plugin like livequery.
You can read some more here http://docs.jquery.com/Plugins/livequery
and get the plugin.

Re: Dynamic img not getting events

by Andrei Eftimie :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You can use jQuery's bundeled .live() method.

$('#ajaxElement').live('click', function(){
  doSomething();
});

On 31 oct., 18:38, JT <josejtam...@...> wrote:
> To bind events, such as 'click', to elements that are added to the
> page dynamically, as with AJAX, you can use a plugin like livequery.
> You can read some more herehttp://docs.jquery.com/Plugins/livequery
> and get the plugin.