You have too much of "this". :-)

Remember that every function you call may have a different "this". Just
because you had a "this" in an outer function with the value you want, it
doesn't mean that some other function you call will have the same "this" -
even if it's nested inside that outer function.

Inside $.get() callback, "this" is definitely not what you expect:

http://docs.jquery.com/Ajax/jQuery.get#urldatacallbacktype

So, to fix it, don't use "this". There are several ways you can do this (pun
intended), but I would suggest using the explicit function arguments that
.each() provides:

    $(".thismonth").each( function( i, monthElement ) {
        $(monthElement).bind("click", function( e ) {
            $.get( "main.php?action=getEventList", function( data ) {
                $("#eventdetails").html( data );
                $(".event").each( function( i, eventElement ) {
                    $(eventElement).bind( "click", function( e ) {
                        $.get( "main.php?action=getEvenDetails",
                            function(data) {
                                $(eventElement).html(data);
                            });
                    });
                });
            });
        });
    });

Now everything is in ordinary JavaScript variables (or function parameters,
which are the same thing). Unlike "this", variables and parameters follow
the scope rules that you expect.

BTW, in the code you posted, there's an extra close paren in each of your
$.get() calls. I assume that was just a typo in the posted code and they are
not present in your actual code?

-Mike

On Tue, Nov 24, 2009 at 6:21 AM, coffeecup <gocoffee...@gmail.com> wrote:

> Hello,
>
> iam experimenting a little bit with jquery and have encountered the
> following problem.
>
> my project is a calendar which can be browsed and is drawed with ajax
> and events can be added, when clicking a day of the current month a
> div pops up which contains the overview of the selected day and lists
> all events. when clicking on a list item a more detailed info of this
> event should appear.
> viewing detailed info should only be possible if logged in.
>
> $(".thismonth").each(function(){ // for every day of the month
>  $(this).bind("click", function(e){  // click handler
>   $.get("main.php?action=getEventList"),function(data){  // get
> eventdata of the selected day
>      $("#eventdetails").html(data);   // display events of the
> selected day
>   $(".event").each(function(){  // for each event in the list
>    $(this).bind("click", function(e){   add click handler for event
> in the list
>      $.get("main.php?action=getEvenDetails"),function(data){   // get
> detailed info if logged in
>       $(this).html(data);  // display detailed info
>     });
> .........
>
> so.. if i skip the 2nd  $.get request and use just $(this).html
> ('test'); it works fine, when using get i get the following error (this
> [0].ownerDocument || this[0]).createDocumentFragment is not a
> function.
>
>
>
>
>
>

Reply via email to