Ok, I'll be more specific to make your life easier :)
The reason your code example doesn't work, is because you start the
load request, but that is asynchronous (the first A in AJAX :)),
and javascript will execute your next statement with 'each' before
the .load is finished, it doesn't wait for the load to finish.
In order to do that you need to add a callback function to the load,
and add your events there:
$('#menu).load('/menu', {}, function() {
$('#level1 a').each(function(){$(this).click(function(){alert
'test'})})
});
On Feb 2, 5:06 pm, Mike McNally <[email protected]> wrote:
> The load() function allows a second parameter that's called after the
> load completes.
>
> On Mon, Feb 2, 2009 at 9:03 AM, Mauricio (Maujor) Samy Silva
>
>
>
>
>
> <[email protected]> wrote:
>
> > Hi HK
> > It doesn't works because elements loaded via AJAX isn't available when the
> > page is just loaded.
> > Before version 1.3.1 to bind events to elements loaded via AJAX you must use
> > the LIVEQUERY plugin.
> > Version (1.3) 1.3.1 added the live() and die() methods that should solve
> > your problem.
>
> > Have a look at:http://docs.jquery.com/Events/live#typefn
>
> > Maurício
>
> > -----Mensagem Original----- De: "HK" <[email protected]>
> > Para: "jQuery (English)" <[email protected]>
> > Enviada em: segunda-feira, 2 de fevereiro de 2009 09:21
> > Assunto: [jQuery] DOM manipoulation after ajax load
>
> >> Hello,
>
> >> I have the following case and I don't know what's happening...
>
> >> I have a sidebar #menu where in document ready I have:
>
> >> $('#menu).load('/menu')
>
> >> #menu is loaded with something like:
> >> <div id="menu">
> >> <div id="level1">
> >> various <a> tags
> >> </div>
>
> >> <div id="level2">
> >> various <a> tags
> >> </div>
>
> >> <div id="level3">
> >> various <a> tags
> >> </div>
>
> >> </div>
>
> >> Now I want to assign to each of the <a> tags some click functions. I
> >> do :
>
> >> $(document).ready(function() {
> >> $('#menu).load('/menu');
> >> $('#level1 a').each(function(){$(this).click(function(){alert
> >> 'test'})})
> >> })
>
> >> But this doesn't work. Can jquery access elements it has loaded with
> >> load ajax function?
>
> >> thanks
>
> --
> Turtle, turtle, on the ground,
> Pink and shiny, turn around.