tags:

views:

17

answers:

2

Hi, How does one go about adding a click handler in the following example? I need to assign it to the newly appended anchor element.

$.each(regions1, function(key, value) { 

    var coords = regions1[key].rel.split('-');
    $("#map").append("<a href='javascript:void(0)' id='"+ regions1[key].id +"' class='bullet' style='left: "+ addpx(Number(coords[0]) - rempx(settings.bulletWidthOffset)) +"; top: "+ addpx(Number(coords[1]) - rempx(settings.bulletHeightOffset)) +"'>&nbsp;</a> ")

    //.click(function(){showPopup(regions1[key].id);})

});

Cheers

+2  A: 

You want to use the .live jQuery keyword.

http://api.jquery.com/live/

$('.bullet').live('click', function() {
  // Bound handler called.
});

This sample, btw, needs to sit outside any of your other code and placed within the $(document).ready jQuery method. It will bind a click event to all items that have a class of "bullet".

griegs
You have 'bind' in the snippet instead of 'live'.
sje397
fixed that thanks.
griegs
D'oh, we must have edited it at the same time.
Robert
Yeah i noticed that too. :)
griegs
A: 

try this...

$.each(regions1, function(key, value) { 

    var coords = regions1[key].rel.split('-');

    // first, create the element
    var element = $("<a href='javascript:void(0)' id='"+ regions1[key].id +"' class='bullet' style='left: "+ addpx(Number(coords[0]) - rempx(settings.bulletWidthOffset)) +"; top: "+ addpx(Number(coords[1]) - rempx(settings.bulletHeightOffset)) +"'>&nbsp;</a> ");

    // then add the listener/handler
    element.click(function(){showPopup(regions1[key].id);})

    // finally, append the new element to the dom.
    $("#map").append( element );
});

cheers!

Lee