Pascal schrieb:
> hey gang--
>
> i got a bit of a stumper for ya, trying to bind a function to the
> click event of each link in a dynamically generated list.
>
> i have a bunch of long content pages that we're breaking up into
> subpages, with a sidenav to navigate through them. the markup for
> each page is as follows:
>
> <div class="page">
> <h3>Page One Title</h3>
> <p>....</p>
> <p>....</p>
> </div>
>
> <div class="page">
> <h3>Another Page Title</h3>
> <p>....</p>
> <p>....</p>
> </div>
>
>
> here's the function that sets up the list of links and stuffs them
> into the sidenav.
>
> $('.page').each(function(i) {
> var title = $('h3',this).eq(0).text();
> var link = $('<li><a href="#page'+(i+1)+'">'+title+'</a></li>');
> $('.sidenav ul').append(link);
> $('a',link).click(function() {
> alert(1);
> // $('.page',pages).hide();
> // $(page).show();
> });
> });
> $('.page').hide().eq(0).show();
>
>
> it grabs the text of the first H3 in each section to use as the link
> text. it builds an LI element containing the link, then appends that
> to the sidenav. then it tries to assign a click handler to the link.
>
> i've commented out the functional part of the click function and just
> put in an alert for testing, but the function isn't firing when i
> click these links. am i missing something?
>
> thanks for any help,
>
> -p
I see one thing that may cause the problem: your variable link is a
jQuery object. This is passed as context a few lines later on, but you
have to pass a DOM node as context to the $ function.
Try this:
$('a', link[0]).click(...);
Or to overall shorten the code:
var link = $('<li> ... </li>').appendTo('.sidenav ul')[0];
$('a', link).click(...);
Does that help?
-- Klaus
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/