Since jQuery 1.3 you can use the live() function, so you don't need to
rebind the events.

Just set $('table caption a').live('click', addItemFinal) once in $
(document).ready() and all anchors added to the doc afterwards that
match this selector will fire the function on click.

live() uses what is called 'event delegation':
http://www.robertnyman.com/2008/05/04/event-delegation-with-javascript/

In your code, you could simply use $('table caption a').unbind
('click') if there are no other event listeners you want to preserve.

cheers,
- ricardo

On Mar 19, 9:33 am, bart <b...@ivwd.nl> wrote:
> I've followed Karl Swedberg's article on rebinding events at
> learningjquery.com. The theory behind this technique is clear to me
> and I've got a working example.
>
> function addItemFinal()
> {
>         var $href = $('table caption a').attr('href');
>         var $rawmaandjaar = $href.split('&');
>         var $maand = $rawmaandjaar[0].split('=');
>         var $jaar = $rawmaandjaar[1].split('=');
>
>         $.get('includes/inc/ajax/fetchtable.php', { maand: $maand[1], jaar:
> $jaar[1] }, function(data)
>         {
>                 $('div#agendawrapper').html(data);
>                 $('table caption a').unbind('click', 
> addItemFinal).bind('click',
> addItemFinal);
>         });
>         return false;
>
> }
>
> $('table caption a').bind('click', addItemFinal);
>
> The code in the function is pretty simple, it takes the href attribute
> and extracts some variables from it. With the variables it makes a GET
> request and in the callback it rebinds again.
>
> However is there more than one anchor in the caption and with the
> current code it just takes "table caption a" whereas this should be
> the actual link you clicked. I guess I should pass something to the
> function but it's used as a reference so I'm not sure how to achieve
> this...
>
> Someone who can help out?

Reply via email to