tags:

views:

57

answers:

2

Hi there,

im trying since a long term to use events on elements in my dom which has been added asynchronus. I´ve read something about bind but is there a different Way to etablish something like this?

For Example I have this situation.

$.getJQUERY(myUrl, {var:value}, function(i, data){
  $.each(data.values, function(value){
      $("body").append('<div id="div_no_'+i+'">'+value+'</div>);
     // Here i dont want to place the EventListeners
  })
});
$("div_no_1").click(function(){
    // do something
});

Could someone help me to find a way to etablish something like this??

Thank you in advance

Great

Bernhard

+1  A: 

You can use a .live() click handler like this:

$("div[id^=div_no_]").live('click', function(){
  // do something
});

This listens for clicks from existing or new elements that have IDs starting with div_no_

You only need this one time, it'll work for all the divs you create.

Jason's comment makes a lot of sense for your situation as well, something like this:

$.getJQUERY(myUrl, {var:value}, function(i, data){
  $.each(data.values, function(value){
      $("body").append('<div id="div_no_'+i+'" class="clickme">'+value+'</div>');
  })
});

$(".clickme").live('click', function(){
  // do something
});
Nick Craver
additionally, you could add a class to each div and bind to that class.
Jason
@Jason - Agreed, that makes this case a lot cleaner/more explicit, updated to include that option.
Nick Craver
.live is great for this, but if you're using jQuery 1.4.2, they've also added .delegateThe reason for .delegate is that the .live events all bind to the document. That's fine for some cases but can really cause slowdown if you use a lot of them. .delegate works similarly, but you're binding to a parent element rather than all the way up to document. http://www.learningjquery.com/2010/03/using-delegate-and-undelegate-in-jquery-1-4-2 has a nice writeup on the distinctions.
Brian Arnold
@Brian - Actually delegate uses `live()` internally (and you could do this even in 1.3.2 by giving live a context). Since he's added the elements to body and clicking on them there, it's 2 bubbles vs 1 bubble, a really, really trivial difference. To be correct, `.delegate()` can attach to any common ancestor, not just the parent (in plain `.live()`, the common ancestor just happens to be document, the default context), it's nothing more than `.live()` with a more local context. If you test it, there's really just an infinitesimally small speed difference, people seem to overestimate it.
Nick Craver
A: 

This is a piggy-back off Nick's answer that may help you understand jQuery and Javascript a bit more:

$.getJQUERY(myUrl, {var:value}, function(i, data){
  $.each(data.values, function(value){
      var myDiv = document.createElement('div');
      $(myDiv).id('div_no_' + i).addClass('clickme').text(value);
      $('body').append(myDiv);
  })
});

$(".clickme").live('click', function(){
  // do something
});

The benefit with doing it this way is that it is more modular, should you decide to make changes/updates in the future. For instance, if you decide that you don't want it to be a div anymore, you can simply change the type of element created to, say, a span and that's the only change you need to make (granted in this example the variable is called myDiv but hopefully you name your variables better than that :). Basically, you're not rendering the HTML directly, but rather abstracting it so that jQuery/JS can do the heavy lifting for you.

Not trying to steal Nick's thunder, just providing an alternative, pedagogical example.

Jason