What you are looking for is "event delegation".

In your last version, you are attaching a "click" event handler to
every <a> you add to your table. It can be time and resource
consuming.

A better way is to bind the "click" event to the container where you
will load your dynamic content and wait for the event to bubble up to
the container. Using jQuery, you can retrieve the clicked <a> using
the "target" property of the event object.

// I assume here that "tb" is not loaded dynamically and is already
here at page load
$(document).ready(function()
{
        $("#tb").bind('click', function(event)
        {
                // Find the clicked <a> in the table.
                // Using closest since user can click on <a>
                // or any element inside of <a>.
                var $a = $(event.target).closest('a');

                // $a contains the clicked <a>
                // Do whatever you want...
        });
});


Denis Caggiano a écrit :
> Now Im get confused...
> Wil, Is the code that you suggested equal that I wrote?
>
> Mine
> $("#tb a").click(function() {
>                alert(this.id);
> });
>
> Wil
> $("#tb a").click(function() {
>        alert('Test');
>  });
>
> Tks

Reply via email to