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);
});
});
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 withoutvarwill maketargetElemglobal, which could still work unless that click handler applies to multiple buttons and they are clicked in quick succession before the ajax calls return.var targetElem = $($(this).attr('data-target'));- declare it as a local variable