On Wed, May 7, 2008 at 2:30 AM, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
>  Hi everybody - new to both this group and jQuery, so please be
>  gentle ;)
>
>  I have some code which roughly looks something like this:
>
>  $(".container ul > li > a").click(
>         // only do the JSON if the a isn't followed by a DL
>         if(!($(this).next("dl").length)){
>                 $.getJSON("json.js", function(data){
>                         htmlString = "<dl><dt>Results:</dt><dd>Output: " + 
> data.title + ",
>  Input: " + data.name + "</dd>;
>                 });
>         )
>  )
>
>  Now, what I want to do is put htmlString after the a-object - $(this)
>  - , but obviously I am out of scope, so I can't just add it like this
>
>  $(this).after(htmlString)
>
>  Any suggestions on how I can do this?
>

Save 'this' in a var first:

$(".container ul > li > a").click(function() {
    // only do the JSON if the a isn't followed by a DL
    var $a = $(this); // save the anchor
    if ( ! ($a.next("dl").length) ) {
        $.getJSON("json.js", function(data) {
            var s = "<dl><dt>Results:</dt><dd>Output: "
                + data.title + ",Input: " + data.name + "</dd>";
            $a.after(s);
        });
    }
});

Reply via email to