1

Apologies if this is a stupid beginners question, but I'm stuck.

I have some jquery code to get some HTML from the server, which I then wish to paste in a provided div. The div is set as data-target. So I use the $($(this).attr('data-target')) context to find the target div. However in the .done() function, I'm no longer in the context and I can't find the target div.

$('.popout-button').click(function (event) {
    event.preventDefault();
    targetElem = $($(this).attr('data-target'));
    //request
    $.get("ajax.php", {
        action: "defInfo",
        class: "Cats22"
    })
        .done(function (data) {
            //Dump the data
            //This doesn't work. Can I pass targetElem somehow?
            targetElem.html(data);
        });
});
4
  • 1
    Why don't you try targetElem = $("#targetelement"); Commented Dec 31, 2013 at 8:53
  • Use var targetElem = ... - that will make the variable local to the .click() handler, and accessible from the .done() handler because it declared inside the .click() handler. Your current code without var will make targetElem global, which could still work unless that click handler applies to multiple buttons and they are clicked in quick succession before the ajax calls return. Commented Dec 31, 2013 at 8:55
  • use var targetElem = $($(this).attr('data-target')); - declare it as a local variable Commented Dec 31, 2013 at 8:55
  • @nnnnnn I think that was the ticket. Thank you. Commented Dec 31, 2013 at 9:37

2 Answers 2

1

If you declare your targetElem at global level i.e. out side the function and then initialize it then I think it will work i.e. you can access it from inside the done function

var targetElem;
$('.popout-button').click(function(event) {
    event.preventDefault();

    targetElem = $($(this).attr('data-target'));

    //request
    $.get("ajax.php", {action: "defInfo", class: "Cats22"})
            .done(function(data) {
                //Dump the data

                //This doesn't work. Can I pass targetElem somehow?
                targetElem.html(data);

            });
});
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to avoid dealing with the scopes and access the element from a lower level, you can add an id to your element, or, if you can't (for whatever the reason), you can add a class name dynamically... like this:

$($(this).attr('data-target')).addClass('target');

Then, just select it again within the done() function like this:

var targetElem = $('.target');
targetElem.html(data);

2 Comments

But why would you want to select it again within the .done() function when you already have a reference to it that is in scope?
@nnnnnn Not sure what's going on in the rest of the code, so this is to avoid potential scoping issues. I hear you, no reason why OP's example shouldn't work, but maybe he wants a different approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.